1use eyre::Result;
2use foundry_common::fs::canonicalize_path;
3use foundry_compilers::{
4 CompilerInput, Graph, Project, ProjectCompileOutput, ProjectPathsConfig,
5 artifacts::{Source, Sources},
6 multi::{MultiCompilerLanguage, MultiCompilerParser},
7 solc::{SOLC_EXTENSIONS, SolcLanguage, SolcVersionedInput},
8};
9use foundry_config::Config;
10use rayon::prelude::*;
11use solar::{interface::MIN_SOLIDITY_VERSION, sema::ParsingContext};
12use std::{
13 collections::HashSet,
14 path::{Path, PathBuf},
15};
16
17#[cfg(windows)]
18use path_slash::PathExt as _;
19#[cfg(windows)]
20use std::os::windows::ffi::OsStrExt as _;
21
22#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
24pub struct SolarSourceStatus {
25 has_compatible_sources: bool,
27 has_unsupported_sources: bool,
29}
30
31impl SolarSourceStatus {
32 pub const fn is_fully_supported(&self) -> bool {
34 self.has_compatible_sources && !self.has_unsupported_sources
35 }
36}
37
38pub fn configure_pcx_all_sources(
42 pcx: &mut ParsingContext<'_>,
43 config: &Config,
44 project: Option<&Project>,
45 target_paths: Option<&[PathBuf]>,
46) -> Result<bool> {
47 let status = configure_pcx_all_sources_with_status(pcx, config, project, target_paths)?;
48 if !status.has_compatible_sources && !status.has_unsupported_sources {
49 eyre::bail!("no Solidity sources");
50 }
51 Ok(status.has_compatible_sources)
52}
53
54pub fn configure_pcx_all_sources_with_status(
57 pcx: &mut ParsingContext<'_>,
58 config: &Config,
59 project: Option<&Project>,
60 target_paths: Option<&[PathBuf]>,
61) -> Result<SolarSourceStatus> {
62 configure_pcx_with_sources(pcx, config, project, target_paths, true)
63}
64
65fn configure_pcx_with_sources(
66 pcx: &mut ParsingContext<'_>,
67 config: &Config,
68 project: Option<&Project>,
69 target_paths: Option<&[PathBuf]>,
70 all_versions: bool,
71) -> Result<SolarSourceStatus> {
72 let project = match project {
74 Some(project) => project,
75 None => &config.ephemeral_project()?,
76 };
77
78 let sources = match target_paths {
79 Some(targets) => {
81 let mut sources = Sources::new();
82 for t in targets {
83 let path = canonicalize_path(t)?;
84 let source = Source::read(&path)?;
85 sources.insert(path, source);
86 }
87 sources
88 }
89 None => {
91 let mut sources = project.paths.read_input_files()?;
92 if let Some(filter) = &project.sparse_output {
93 sources.retain(|path, _| filter.is_match(path));
94 }
95 sources
96 }
97 };
98
99 let graph = Graph::<MultiCompilerParser>::resolve_sources(&project.paths, sources)?;
101 let Some(versioned_sources) = graph
102 .into_sources_by_version(project)?
104 .sources
105 .into_iter()
106 .find(|(lang, _)| *lang == MultiCompilerLanguage::Solc(SolcLanguage::Solidity))
108 .map(|(_, sources)| sources)
109 else {
110 return Ok(SolarSourceStatus::default());
111 };
112
113 let mut has_unsupported_sources = false;
114 let versioned_sources = versioned_sources.into_iter().filter(|(version, sources, _)| {
115 if version < &MIN_SOLIDITY_VERSION && !sources.is_empty() {
116 has_unsupported_sources = true;
117 false
118 } else {
119 true
120 }
121 });
122 let (version, sources) = if all_versions {
123 versioned_sources.fold(
124 (MIN_SOLIDITY_VERSION, Sources::default()),
125 |(version, mut sources), (source_version, version_sources, _)| {
126 sources.extend(version_sources);
127 (version.max(source_version), sources)
128 },
129 )
130 } else {
131 versioned_sources
132 .max_by(|(v1, _, _), (v2, _, _)| v1.cmp(v2))
133 .map_or((MIN_SOLIDITY_VERSION, Sources::default()), |(v, s, _)| (v, s))
134 };
135
136 let has_sources = !sources.is_empty();
137 if !all_versions && !has_sources {
138 sh_warn!("no files found. Solar doesn't support Solidity versions prior to 0.8.0")?;
139 }
140
141 let solc = SolcVersionedInput::build(
142 sources,
143 config.solc_settings()?,
144 SolcLanguage::Solidity,
145 version,
146 );
147
148 configure_pcx_from_solc(pcx, &project.paths, &solc, true);
149
150 Ok(SolarSourceStatus { has_compatible_sources: has_sources, has_unsupported_sources })
151}
152
153pub fn get_solar_sources_from_compile_output(
160 config: &Config,
161 output: &ProjectCompileOutput,
162 target_paths: Option<&[PathBuf]>,
163 ignored_paths: Option<&[PathBuf]>,
164) -> Result<SolcVersionedInput> {
165 let is_solidity_file = |path: &Path| -> bool {
166 path.extension().and_then(|s| s.to_str()).is_some_and(|ext| SOLC_EXTENSIONS.contains(&ext))
167 };
168
169 let is_ignored = |path: &Path| -> bool {
170 ignored_paths.is_some_and(|ignored| ignored.iter().any(|ignored_path| path == ignored_path))
171 };
172
173 let mut source_paths: HashSet<PathBuf> = if let Some(targets) = target_paths
175 && !targets.is_empty()
176 {
177 let mut source_paths = HashSet::new();
178 for path in targets.iter().filter_map(|path| {
179 is_solidity_file(path).then(|| canonicalize_path(path).ok()).flatten()
180 }) {
181 if source_paths.insert(path.clone()) {
182 source_paths.extend(
184 output
185 .graph()
186 .imports(&path)
187 .into_iter()
188 .filter(|import| !is_ignored(import))
189 .map(Path::to_path_buf),
190 );
191 }
192 }
193
194 source_paths
195 } else {
196 output
197 .graph()
198 .source_files()
199 .filter_map(|idx| {
200 let path = output.graph().node_path(idx).to_path_buf();
201 is_solidity_file(&path).then_some(path)
202 })
203 .collect()
204 };
205
206 let (version, sources) = {
208 let (mut max_version, mut sources) = (MIN_SOLIDITY_VERSION, Sources::new());
209 for (id, _) in output.artifact_ids() {
210 if let Ok(path) = canonicalize_path(&id.source)
211 && source_paths.remove(&path)
212 {
213 if id.version < MIN_SOLIDITY_VERSION {
214 continue;
215 } else if max_version < id.version {
216 max_version = id.version;
217 };
218
219 let source = Source::read(&path)?;
220 sources.insert(path, source);
221 }
222 }
223
224 (max_version, sources)
225 };
226
227 let solc = SolcVersionedInput::build(
228 sources,
229 config.solc_settings()?,
230 SolcLanguage::Solidity,
231 version,
232 );
233
234 Ok(solc)
235}
236
237pub fn configure_pcx_from_compile_output(
239 pcx: &mut ParsingContext<'_>,
240 config: &Config,
241 output: &ProjectCompileOutput,
242) -> Result<()> {
243 let solc = get_solar_sources_from_compile_output(config, output, None, None)?;
244 configure_pcx_from_solc(pcx, &config.project_paths(), &solc, true);
245 Ok(())
246}
247
248#[cfg(windows)]
251fn solar_slash_path(path: &Path) -> String {
252 let has_trailing_separator = path
253 .as_os_str()
254 .encode_wide()
255 .last()
256 .is_some_and(|c| c == u16::from(b'/') || c == u16::from(b'\\'));
257 let mut path = path.to_slash_lossy().into_owned();
258 if has_trailing_separator && !path.ends_with('/') {
259 path.push('/');
260 }
261 path
262}
263
264pub fn configure_pcx_from_solc(
269 pcx: &mut ParsingContext<'_>,
270 project_paths: &ProjectPathsConfig,
271 vinput: &SolcVersionedInput,
272 add_source_files: bool,
273) {
274 configure_pcx_from_solc_cli(pcx, project_paths, &vinput.cli_settings);
275 if add_source_files {
276 let sources = vinput
277 .input
278 .sources
279 .par_iter()
280 .filter_map(|(path, source)| {
281 #[cfg(windows)]
282 let path = PathBuf::from(solar_slash_path(path));
283 #[cfg(not(windows))]
284 let path = path.clone();
285 pcx.sess.source_map().new_source_file(path, source.content.as_str()).ok()
286 })
287 .collect::<Vec<_>>();
288 pcx.add_files(sources);
289 }
290}
291
292fn configure_pcx_from_solc_cli(
293 pcx: &mut ParsingContext<'_>,
294 project_paths: &ProjectPathsConfig,
295 cli_settings: &foundry_compilers::solc::CliSettings,
296) {
297 pcx.dcx().set_allowed_diagnostic_codes(["5333".into()]);
299 let base_path = cli_settings.base_path.as_ref().unwrap_or(&project_paths.root);
300 pcx.file_resolver.set_base_path(base_path);
301 pcx.file_resolver.set_current_dir(base_path);
302 for remapping in &project_paths.remappings {
303 let context = remapping.context.clone().unwrap_or_default();
304 #[cfg(windows)]
307 let context = solar_slash_path(Path::new(&context));
308 pcx.file_resolver.add_import_remapping(solar::sema::interface::config::ImportRemapping {
309 context,
310 prefix: remapping.name.clone(),
311 path: remapping.path.clone(),
312 });
313 }
314 pcx.file_resolver.add_include_paths(cli_settings.include_paths.iter().cloned());
315}
316
317#[cfg(all(test, windows))]
318mod tests {
319 use super::*;
320
321 #[test]
322 fn solar_slash_path_preserves_windows_prefixes_and_boundaries() {
323 for (path, expected) in [
324 (r"lib\outer\", "lib/outer/"),
325 (r"\\server\share\project/", r"\\server\share/project/"),
326 (r"\\?\C:\project/", r"\\?\C:/project/"),
327 (r"\\?\UNC\server\share\project/", r"\\?\UNC\server\share/project/"),
328 ] {
329 assert_eq!(solar_slash_path(Path::new(path)), expected);
330 }
331 }
332}