3
3
import re
4
4
import subprocess
5
5
import warnings
6
+ import shlex
6
7
7
8
import yaml
8
9
@@ -40,17 +41,22 @@ def get_solidity():
40
41
return solc_wrapper
41
42
42
43
43
- def solc_arguments (libraries = None , combined = 'bin,abi' , optimize = True , extra_args = '' ):
44
+ def solc_arguments (libraries = None , combined = 'bin,abi' , optimize = True , extra_args = None ):
44
45
""" Build the arguments to call the solc binary. """
45
46
args = [
46
47
'--combined-json' , combined ,
47
- '--add-std' ,
48
- extra_args
48
+ '--add-std'
49
49
]
50
50
51
51
if optimize :
52
52
args .append ('--optimize' )
53
53
54
+ if extra_args :
55
+ try :
56
+ args .extend (shlex .split (extra_args ))
57
+ except : # if not a parseable string then treat it as a list
58
+ args .extend (extra_args )
59
+
54
60
if libraries is not None and len (libraries ):
55
61
addresses = [
56
62
'{name}:{address}' .format (name = name , address = address .decode ('utf8' ))
@@ -66,6 +72,10 @@ def solc_arguments(libraries=None, combined='bin,abi', optimize=True, extra_args
66
72
67
73
def solc_parse_output (compiler_output ):
68
74
""" Parses the compiler output. """
75
+ # At the moment some solc output like --hashes or -- gas will not output
76
+ # json at all so if used with those arguments the logic here will break.
77
+ # Perhaps solidity will slowly switch to a json only output and this comment
78
+ # can eventually go away and we will not need to add more logic here at all.
69
79
result = yaml .safe_load (compiler_output )['contracts' ]
70
80
71
81
if 'bin' in tuple (result .values ())[0 ]:
@@ -222,7 +232,7 @@ def solidity_unresolved_symbols(hex_code):
222
232
return set (re .findall (r"_.{39}" , hex_code ))
223
233
224
234
225
- def compile_file (filepath , libraries = None , combined = 'bin,abi' , optimize = True , extra_args = '' ):
235
+ def compile_file (filepath , libraries = None , combined = 'bin,abi' , optimize = True , extra_args = None ):
226
236
""" Return the compile contract code.
227
237
228
238
Args:
@@ -246,7 +256,7 @@ def compile_file(filepath, libraries=None, combined='bin,abi', optimize=True, ex
246
256
return solc_parse_output (output )
247
257
248
258
249
- def compile_contract (filepath , contract_name , libraries = None , combined = 'bin,abi' , optimize = True , extra_args = '' ):
259
+ def compile_contract (filepath , contract_name , libraries = None , combined = 'bin,abi' , optimize = True , extra_args = None ):
250
260
all_contracts = compile_file (
251
261
filepath ,
252
262
libraries = libraries ,
@@ -258,7 +268,7 @@ def compile_contract(filepath, contract_name, libraries=None, combined='bin,abi'
258
268
return all_contracts [contract_name ]
259
269
260
270
261
- def compile_last_contract (filepath , libraries = None , combined = 'bin,abi' , optimize = True , extra_args = '' ):
271
+ def compile_last_contract (filepath , libraries = None , combined = 'bin,abi' , optimize = True , extra_args = None ):
262
272
with open (filepath ) as handler :
263
273
all_names = solidity_names (handler .read ())
264
274
@@ -279,7 +289,7 @@ def compile_last_contract(filepath, libraries=None, combined='bin,abi', optimize
279
289
)
280
290
281
291
282
- def compile_code (sourcecode , libraries = None , combined = 'bin,abi' , optimize = True , extra_args = '' ):
292
+ def compile_code (sourcecode , libraries = None , combined = 'bin,abi' , optimize = True , extra_args = None ):
283
293
args = solc_arguments (libraries = libraries , combined = combined , optimize = optimize , extra_args = extra_args )
284
294
args .insert (0 , get_compiler_path ())
285
295
@@ -323,37 +333,40 @@ def _code_or_path(sourcecode, path, contract_name, libraries, combined, extra_ar
323
333
return result [last_contract ]
324
334
325
335
@classmethod
326
- def compile (cls , code , path = None , libraries = None , contract_name = '' , extra_args = '' ):
336
+ def compile (cls , code , path = None , libraries = None , contract_name = '' , extra_args = None ):
327
337
""" Return the binary of last contract in code. """
328
338
result = cls ._code_or_path (code , path , contract_name , libraries , 'bin' , extra_args )
329
339
return result ['bin' ]
330
340
331
341
@classmethod
332
- def mk_full_signature (cls , code , path = None , libraries = None , contract_name = '' , extra_args = '' ):
342
+ def mk_full_signature (cls , code , path = None , libraries = None , contract_name = '' , extra_args = None ):
333
343
"returns signature of last contract in code"
334
344
335
345
result = cls ._code_or_path (code , path , contract_name , libraries , 'abi' , extra_args )
336
346
return result ['abi' ]
337
347
338
348
@classmethod
339
- def combined (cls , code , path = None ):
349
+ def combined (cls , code , path = None , extra_args = None ):
340
350
""" Compile combined-json with abi,bin,devdoc,userdoc.
341
351
342
352
@param code: literal solidity code as a string.
343
- @param path: absolute path to solidity-file. Note: code & path are exclusive!
353
+ @param path: absolute path to solidity-file. Note: code & path are
354
+ mutually exclusive!
355
+ @param extra_args: Either a space separated string or a list of extra
356
+ arguments to be passed to the solidity compiler.
344
357
"""
345
358
346
359
if code and path :
347
360
raise ValueError ('sourcecode and path are mutually exclusive.' )
348
361
349
362
if path :
350
- contracts = compile_file (path )
363
+ contracts = compile_file (path , extra_args = extra_args )
351
364
352
365
with open (path ) as handler :
353
366
code = handler .read ()
354
367
355
368
elif code :
356
- contracts = compile_code (code )
369
+ contracts = compile_code (code , extra_args = extra_args )
357
370
358
371
else :
359
372
raise ValueError ('either code or path needs to be supplied.' )
@@ -364,7 +377,7 @@ def combined(cls, code, path=None):
364
377
return sorted_contracts
365
378
366
379
@classmethod
367
- def compile_rich (cls , code , path = None ):
380
+ def compile_rich (cls , code , path = None , extra_args = None ):
368
381
"""full format as returned by jsonrpc"""
369
382
370
383
return {
@@ -381,7 +394,7 @@ def compile_rich(cls, code, path=None):
381
394
},
382
395
}
383
396
for contract_name , contract
384
- in cls .combined (code , path = path )
397
+ in cls .combined (code , path = path , extra_args = extra_args )
385
398
}
386
399
387
400
0 commit comments