@@ -146,6 +146,7 @@ def prepare_compiler_input( # pylint: disable=too-many-arguments
146146 force_no_optimize_yul : bool ,
147147 interface : CompilerInterface ,
148148 smt_use : SMTUse ,
149+ metadata_option_supported : bool ,
149150) -> Tuple [List [str ], str ]:
150151
151152 if interface == CompilerInterface .STANDARD_JSON :
@@ -168,7 +169,9 @@ def prepare_compiler_input( # pylint: disable=too-many-arguments
168169 else :
169170 assert interface == CompilerInterface .CLI
170171
171- compiler_options = [str (source_file_name ), '--bin' , '--metadata' ]
172+ compiler_options = [str (source_file_name ), '--bin' ]
173+ if metadata_option_supported :
174+ compiler_options .append ('--metadata' )
172175 if optimize :
173176 compiler_options .append ('--optimize' )
174177 elif force_no_optimize_yul :
@@ -182,13 +185,37 @@ def prepare_compiler_input( # pylint: disable=too-many-arguments
182185 return (command_line , compiler_input )
183186
184187
188+ def detect_metadata_cli_option_support (compiler_path : Path ):
189+ process = subprocess .run (
190+ [str (compiler_path ), '--metadata' , '-' ],
191+ input = "contract C {}" ,
192+ encoding = 'utf8' ,
193+ capture_output = True ,
194+ check = False ,
195+ )
196+
197+ negative_response = "unrecognised option '--metadata'" .strip ()
198+ if (process .returncode == 0 ) != (process .stderr != negative_response ):
199+ # If the error is other than expected or there's an error message but no error, don't try
200+ # to guess. Just fail.
201+ print (
202+ f"Compiler exit code: { process .returncode } \n "
203+ f"Compiler output:\n { process .stderr } \n " ,
204+ file = sys .stderr
205+ )
206+ raise Exception ("Failed to determine if the compiler supports the --metadata option." )
207+
208+ return process .returncode == 0
209+
210+
185211def run_compiler ( # pylint: disable=too-many-arguments
186212 compiler_path : Path ,
187213 source_file_name : Path ,
188214 optimize : bool ,
189215 force_no_optimize_yul : bool ,
190216 interface : CompilerInterface ,
191217 smt_use : SMTUse ,
218+ metadata_option_supported : bool ,
192219 tmp_dir : Path ,
193220) -> FileReport :
194221
@@ -200,6 +227,7 @@ def run_compiler( # pylint: disable=too-many-arguments
200227 force_no_optimize_yul ,
201228 interface ,
202229 smt_use ,
230+ metadata_option_supported ,
203231 )
204232
205233 process = subprocess .run (
@@ -222,6 +250,7 @@ def run_compiler( # pylint: disable=too-many-arguments
222250 force_no_optimize_yul ,
223251 interface ,
224252 smt_use ,
253+ metadata_option_supported ,
225254 )
226255
227256 # Create a copy that we can use directly with the CLI interface
@@ -249,6 +278,8 @@ def generate_report(
249278 smt_use : SMTUse ,
250279 force_no_optimize_yul : bool
251280):
281+ metadata_option_supported = detect_metadata_cli_option_support (compiler_path )
282+
252283 with open ('report.txt' , mode = 'w' , encoding = 'utf8' , newline = '\n ' ) as report_file :
253284 for optimize in [False , True ]:
254285 with TemporaryDirectory (prefix = 'prepare_report-' ) as tmp_dir :
@@ -261,6 +292,7 @@ def generate_report(
261292 force_no_optimize_yul ,
262293 interface ,
263294 smt_use ,
295+ metadata_option_supported ,
264296 Path (tmp_dir ),
265297 )
266298 report_file .write (report .format_report ())
0 commit comments