Skip to content

Commit 44c91b2

Browse files
zhiqiangxusadoci
authored andcommitted
common/compiler: add extra include paths to solidity compiler (ethereum#24541)
This PR adds a ExtraAllowedPath field to Solidity and exposes two APIs: CompileSource and CompileFiles, which were hidden inside CompileSolidityString and CompileSolidity before.
1 parent 0c2b4ea commit 44c91b2

File tree

1 file changed

+28
-8
lines changed

1 file changed

+28
-8
lines changed

common/compiler/solidity.go

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import (
3131
type Solidity struct {
3232
Path, Version, FullVersion string
3333
Major, Minor, Patch int
34+
ExtraAllowedPath []string
3435
}
3536

3637
// --combined-output format
@@ -58,11 +59,19 @@ type solcOutputV8 struct {
5859
Version string
5960
}
6061

62+
func (s *Solidity) allowedPaths() string {
63+
paths := []string{".", "./", "../"} // default to support relative paths
64+
if len(s.ExtraAllowedPath) > 0 {
65+
paths = append(paths, s.ExtraAllowedPath...)
66+
}
67+
return strings.Join(paths, ", ")
68+
}
69+
6170
func (s *Solidity) makeArgs() []string {
6271
p := []string{
6372
"--combined-json", "bin,bin-runtime,srcmap,srcmap-runtime,abi,userdoc,devdoc",
64-
"--optimize", // code optimizer switched on
65-
"--allow-paths", "., ./, ../", // default to support relative paths
73+
"--optimize", // code optimizer switched on
74+
"--allow-paths", s.allowedPaths(),
6675
}
6776
if s.Major > 0 || s.Minor > 4 || s.Patch > 6 {
6877
p[1] += ",metadata,hashes"
@@ -108,22 +117,33 @@ func CompileSolidityString(solc, source string) (map[string]*Contract, error) {
108117
if err != nil {
109118
return nil, err
110119
}
111-
args := append(s.makeArgs(), "--")
112-
cmd := exec.Command(s.Path, append(args, "-")...)
113-
cmd.Stdin = strings.NewReader(source)
114-
return s.run(cmd, source)
120+
return s.CompileSource(source)
115121
}
116122

117123
// CompileSolidity compiles all given Solidity source files.
118124
func CompileSolidity(solc string, sourcefiles ...string) (map[string]*Contract, error) {
119125
if len(sourcefiles) == 0 {
120126
return nil, errors.New("solc: no source files")
121127
}
122-
source, err := slurpFiles(sourcefiles)
128+
s, err := SolidityVersion(solc)
123129
if err != nil {
124130
return nil, err
125131
}
126-
s, err := SolidityVersion(solc)
132+
133+
return s.CompileFiles(sourcefiles...)
134+
}
135+
136+
// CompileSource builds and returns all the contracts contained within a source string.
137+
func (s *Solidity) CompileSource(source string) (map[string]*Contract, error) {
138+
args := append(s.makeArgs(), "--")
139+
cmd := exec.Command(s.Path, append(args, "-")...)
140+
cmd.Stdin = strings.NewReader(source)
141+
return s.run(cmd, source)
142+
}
143+
144+
// CompileFiles compiles all given Solidity source files.
145+
func (s *Solidity) CompileFiles(sourcefiles ...string) (map[string]*Contract, error) {
146+
source, err := slurpFiles(sourcefiles)
127147
if err != nil {
128148
return nil, err
129149
}

0 commit comments

Comments
 (0)