@@ -19,6 +19,7 @@ package compiler
1919import (
2020 "bytes"
2121 "encoding/json"
22+ "errors"
2223 "fmt"
2324 "io/ioutil"
2425 "os"
@@ -110,95 +111,82 @@ func (sol *Solidity) Version() string {
110111 return sol .version
111112}
112113
113- func (sol * Solidity ) Compile (source string ) (contracts map [string ]* Contract , err error ) {
114-
114+ // Compile builds and returns all the contracts contained within a source string.
115+ func (sol * Solidity ) Compile (source string ) (map [string ]* Contract , error ) {
116+ // Short circuit if no source code was specified
115117 if len (source ) == 0 {
116- err = fmt .Errorf ("empty source" )
117- return
118+ return nil , errors .New ("solc: empty source string" )
118119 }
119-
120+ // Create a safe place to dump compilation output
120121 wd , err := ioutil .TempDir ("" , "solc" )
121122 if err != nil {
122- return
123+ return nil , fmt . Errorf ( "solc: failed to create temporary build folder: %v" , err )
123124 }
124125 defer os .RemoveAll (wd )
125126
126- in := strings . NewReader ( source )
127- var out bytes.Buffer
128- // cwd set to temp dir
127+ // Assemble the compiler command, change to the temp folder and capture any errors
128+ stderr := new ( bytes.Buffer )
129+
129130 cmd := exec .Command (sol .solcPath , params ... )
130131 cmd .Dir = wd
131- cmd .Stdin = in
132- cmd .Stdout = & out
133- err = cmd .Run ()
134- if err != nil {
135- err = fmt .Errorf ("solc error: %v" , err )
136- return
137- }
132+ cmd .Stdin = strings .NewReader (source )
133+ cmd .Stderr = stderr
138134
135+ if err := cmd .Run (); err != nil {
136+ return nil , fmt .Errorf ("solc: %v\n %s" , err , string (stderr .Bytes ()))
137+ }
138+ // Sanity check that something was actually built
139139 matches , _ := filepath .Glob (wd + "/*.binary" )
140140 if len (matches ) < 1 {
141- err = fmt .Errorf ("solc error: missing code output" )
142- return
141+ return nil , fmt .Errorf ("solc: no build results found" )
143142 }
144-
145- contracts = make (map [string ]* Contract )
143+ // Compilation succeeded, assemble and return the contracts
144+ contracts : = make (map [string ]* Contract )
146145 for _ , path := range matches {
147146 _ , file := filepath .Split (path )
148147 base := strings .Split (file , "." )[0 ]
149148
150- codeFile := filepath .Join (wd , base + ".binary" )
151- abiDefinitionFile := filepath .Join (wd , base + ".abi" )
152- userDocFile := filepath .Join (wd , base + ".docuser" )
153- developerDocFile := filepath .Join (wd , base + ".docdev" )
154-
155- var code , abiDefinitionJson , userDocJson , developerDocJson []byte
156- code , err = ioutil .ReadFile (codeFile )
157- if err != nil {
158- err = fmt .Errorf ("error reading compiler output for code: %v" , err )
159- return
149+ // Parse the individual compilation results (code binary, ABI definitions, user and dev docs)
150+ var binary []byte
151+ if binary , err = ioutil .ReadFile (filepath .Join (wd , base + ".binary" )); err != nil {
152+ return nil , fmt .Errorf ("solc: error reading compiler output for code: %v" , err )
160153 }
161- abiDefinitionJson , err = ioutil .ReadFile (abiDefinitionFile )
162- if err != nil {
163- err = fmt .Errorf ("error reading compiler output for abiDefinition: %v" , err )
164- return
165- }
166- var abiDefinition interface {}
167- err = json .Unmarshal (abiDefinitionJson , & abiDefinition )
168154
169- userDocJson , err = ioutil .ReadFile (userDocFile )
170- if err != nil {
171- err = fmt .Errorf ("error reading compiler output for userDoc: %v" , err )
172- return
155+ var abi interface {}
156+ if blob , err := ioutil .ReadFile (filepath .Join (wd , base + ".abi" )); err != nil {
157+ return nil , fmt .Errorf ("solc: error reading abi definition: %v" , err )
158+ } else if err = json .Unmarshal (blob , & abi ); err != nil {
159+ return nil , fmt .Errorf ("solc: error parsing abi definition: %v" , err )
173160 }
174- var userDoc interface {}
175- err = json .Unmarshal (userDocJson , & userDoc )
176161
177- developerDocJson , err = ioutil .ReadFile (developerDocFile )
178- if err != nil {
179- err = fmt .Errorf ("error reading compiler output for developerDoc: %v" , err )
180- return
162+ var userdoc interface {}
163+ if blob , err := ioutil .ReadFile (filepath .Join (wd , base + ".docuser" )); err != nil {
164+ return nil , fmt .Errorf ("solc: error reading user doc: %v" , err )
165+ } else if err = json .Unmarshal (blob , & userdoc ); err != nil {
166+ return nil , fmt .Errorf ("solc: error parsing user doc: %v" , err )
181167 }
182- var developerDoc interface {}
183- err = json .Unmarshal (developerDocJson , & developerDoc )
184168
185- contract := & Contract {
186- Code : "0x" + string (code ),
169+ var devdoc interface {}
170+ if blob , err := ioutil .ReadFile (filepath .Join (wd , base + ".docdev" )); err != nil {
171+ return nil , fmt .Errorf ("solc: error reading dev doc: %v" , err )
172+ } else if err = json .Unmarshal (blob , & devdoc ); err != nil {
173+ return nil , fmt .Errorf ("solc: error parsing dev doc: %v" , err )
174+ }
175+ // Assemble the final contract
176+ contracts [base ] = & Contract {
177+ Code : "0x" + string (binary ),
187178 Info : ContractInfo {
188179 Source : source ,
189180 Language : "Solidity" ,
190181 LanguageVersion : languageVersion ,
191182 CompilerVersion : sol .version ,
192- AbiDefinition : abiDefinition ,
193- UserDoc : userDoc ,
194- DeveloperDoc : developerDoc ,
183+ AbiDefinition : abi ,
184+ UserDoc : userdoc ,
185+ DeveloperDoc : devdoc ,
195186 },
196187 }
197-
198- contracts [base ] = contract
199188 }
200-
201- return
189+ return contracts , nil
202190}
203191
204192func SaveInfo (info * ContractInfo , filename string ) (contenthash common.Hash , err error ) {
0 commit comments