1717from solc_select .solc_select import (
1818 install_artifacts ,
1919 installed_versions ,
20- current_version ,
2120 artifact_path ,
2221)
2322from crytic_compile .compilation_unit import CompilationUnit
@@ -86,6 +85,31 @@ def _extract_libraries(libraries_str: Optional[str]) -> Optional[Dict[str, int]]
8685 return ret
8786
8887
88+ def _configure_solc (solc_requested : str , offline : bool ) -> str :
89+ """
90+ Determine which solc binary to use based on the requested version or path (e.g. '0.8.0' or '/usr/bin/solc-0.8.0').
91+
92+ Args:
93+ solc_requested (str): solc version or path
94+ offline (bool): whether to allow network requests
95+
96+ Returns:
97+ str: path to solc binary
98+ """
99+ if Path (solc_requested ).exists ():
100+ solc_path = Path (solc_requested )
101+ else :
102+ solc_version = solc_requested
103+ if solc_requested in installed_versions ():
104+ solc_path = artifact_path (solc_requested )
105+ else :
106+ # Respect foundry offline option and skip installation.
107+ if not offline :
108+ install_artifacts ([solc_version ])
109+ solc_path = artifact_path (solc_version )
110+ return solc_path .absolute ().as_posix ()
111+
112+
89113# pylint: disable=too-many-instance-attributes
90114class CryticCompile :
91115 """
@@ -139,7 +163,7 @@ def __init__(self, target: Union[str, AbstractPlatform], **kwargs: str) -> None:
139163 ),
140164 None ,
141165 )
142- # If no platform has been found or if it's a Solc we can't do anything
166+ # If no platform has been found or if it's the Solc platform, we can't automatically compile.
143167 if platform_wd and not isinstance (platform_wd , Solc ):
144168 platform_config = platform_wd .config (str (self ._working_dir ))
145169 if platform_config :
@@ -148,18 +172,13 @@ def __init__(self, target: Union[str, AbstractPlatform], **kwargs: str) -> None:
148172
149173 if platform_config .remappings :
150174 kwargs ["solc_remaps" ] = platform_config .remappings
151- if (
152- platform_config .solc_version
153- and platform_config .solc_version != current_version ()[0 ]
154- ):
155- solc_version = platform_config .solc_version
156- if solc_version in installed_versions ():
157- kwargs ["solc" ] = str (artifact_path (solc_version ).absolute ())
158- else :
159- # Respect foundry offline option and don't install a missing solc version
160- if not platform_config .offline :
161- install_artifacts ([solc_version ])
162- kwargs ["solc" ] = str (artifact_path (solc_version ).absolute ())
175+ if platform_config .solc_version is None :
176+ message = f"Could not detect solc version from { platform_wd .NAME } config. Falling back to system version..."
177+ LOGGER .warning (message )
178+ else :
179+ kwargs ["solc" ] = _configure_solc (
180+ platform_config .solc_version , platform_config .offline
181+ )
163182 if platform_config .optimizer :
164183 kwargs ["solc_args" ] += "--optimize"
165184 if platform_config .optimizer_runs :
0 commit comments