diff options
Diffstat (limited to 'crates/project_model/src/workspace.rs')
-rw-r--r-- | crates/project_model/src/workspace.rs | 552 |
1 files changed, 552 insertions, 0 deletions
diff --git a/crates/project_model/src/workspace.rs b/crates/project_model/src/workspace.rs new file mode 100644 index 000000000..a71f96164 --- /dev/null +++ b/crates/project_model/src/workspace.rs | |||
@@ -0,0 +1,552 @@ | |||
1 | //! Handles lowering of build-system specific workspace information (`cargo | ||
2 | //! metadata` or `rust-project.json`) into representation stored in the salsa | ||
3 | //! database -- `CrateGraph`. | ||
4 | |||
5 | use std::{fmt, fs, path::Component, process::Command}; | ||
6 | |||
7 | use anyhow::{Context, Result}; | ||
8 | use base_db::{CrateDisplayName, CrateGraph, CrateId, CrateName, Edition, Env, FileId}; | ||
9 | use cfg::CfgOptions; | ||
10 | use paths::{AbsPath, AbsPathBuf}; | ||
11 | use proc_macro_api::ProcMacroClient; | ||
12 | use rustc_hash::{FxHashMap, FxHashSet}; | ||
13 | |||
14 | use crate::{ | ||
15 | cargo_workspace, cfg_flag::CfgFlag, sysroot::SysrootCrate, utf8_stdout, CargoConfig, | ||
16 | CargoWorkspace, ProjectJson, ProjectManifest, Sysroot, TargetKind, | ||
17 | }; | ||
18 | |||
19 | /// `PackageRoot` describes a package root folder. | ||
20 | /// Which may be an external dependency, or a member of | ||
21 | /// the current workspace. | ||
22 | #[derive(Debug, Clone, Eq, PartialEq, Hash)] | ||
23 | pub struct PackageRoot { | ||
24 | /// Is a member of the current workspace | ||
25 | pub is_member: bool, | ||
26 | pub include: Vec<AbsPathBuf>, | ||
27 | pub exclude: Vec<AbsPathBuf>, | ||
28 | } | ||
29 | |||
30 | #[derive(Clone, Eq, PartialEq)] | ||
31 | pub enum ProjectWorkspace { | ||
32 | /// Project workspace was discovered by running `cargo metadata` and `rustc --print sysroot`. | ||
33 | Cargo { cargo: CargoWorkspace, sysroot: Sysroot, rustc: Option<CargoWorkspace> }, | ||
34 | /// Project workspace was manually specified using a `rust-project.json` file. | ||
35 | Json { project: ProjectJson, sysroot: Option<Sysroot> }, | ||
36 | } | ||
37 | |||
38 | impl fmt::Debug for ProjectWorkspace { | ||
39 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
40 | match self { | ||
41 | ProjectWorkspace::Cargo { cargo, sysroot, rustc } => f | ||
42 | .debug_struct("Cargo") | ||
43 | .field("n_packages", &cargo.packages().len()) | ||
44 | .field("n_sysroot_crates", &sysroot.crates().len()) | ||
45 | .field( | ||
46 | "n_rustc_compiler_crates", | ||
47 | &rustc.as_ref().map_or(0, |rc| rc.packages().len()), | ||
48 | ) | ||
49 | .finish(), | ||
50 | ProjectWorkspace::Json { project, sysroot } => { | ||
51 | let mut debug_struct = f.debug_struct("Json"); | ||
52 | debug_struct.field("n_crates", &project.n_crates()); | ||
53 | if let Some(sysroot) = sysroot { | ||
54 | debug_struct.field("n_sysroot_crates", &sysroot.crates().len()); | ||
55 | } | ||
56 | debug_struct.finish() | ||
57 | } | ||
58 | } | ||
59 | } | ||
60 | } | ||
61 | |||
62 | impl ProjectWorkspace { | ||
63 | pub fn load(manifest: ProjectManifest, config: &CargoConfig) -> Result<ProjectWorkspace> { | ||
64 | let res = match manifest { | ||
65 | ProjectManifest::ProjectJson(project_json) => { | ||
66 | let file = fs::read_to_string(&project_json).with_context(|| { | ||
67 | format!("Failed to read json file {}", project_json.display()) | ||
68 | })?; | ||
69 | let data = serde_json::from_str(&file).with_context(|| { | ||
70 | format!("Failed to deserialize json file {}", project_json.display()) | ||
71 | })?; | ||
72 | let project_location = project_json.parent().unwrap().to_path_buf(); | ||
73 | let project_json = ProjectJson::new(&project_location, data); | ||
74 | ProjectWorkspace::load_inline(project_json)? | ||
75 | } | ||
76 | ProjectManifest::CargoToml(cargo_toml) => { | ||
77 | let cargo_version = utf8_stdout({ | ||
78 | let mut cmd = Command::new(toolchain::cargo()); | ||
79 | cmd.arg("--version"); | ||
80 | cmd | ||
81 | })?; | ||
82 | |||
83 | let cargo = CargoWorkspace::from_cargo_metadata(&cargo_toml, config).with_context( | ||
84 | || { | ||
85 | format!( | ||
86 | "Failed to read Cargo metadata from Cargo.toml file {}, {}", | ||
87 | cargo_toml.display(), | ||
88 | cargo_version | ||
89 | ) | ||
90 | }, | ||
91 | )?; | ||
92 | let sysroot = if config.no_sysroot { | ||
93 | Sysroot::default() | ||
94 | } else { | ||
95 | Sysroot::discover(&cargo_toml).with_context(|| { | ||
96 | format!( | ||
97 | "Failed to find sysroot for Cargo.toml file {}. Is rust-src installed?", | ||
98 | cargo_toml.display() | ||
99 | ) | ||
100 | })? | ||
101 | }; | ||
102 | |||
103 | let rustc = if let Some(rustc_dir) = &config.rustc_source { | ||
104 | Some(CargoWorkspace::from_cargo_metadata(&rustc_dir, config).with_context( | ||
105 | || format!("Failed to read Cargo metadata for Rust sources"), | ||
106 | )?) | ||
107 | } else { | ||
108 | None | ||
109 | }; | ||
110 | |||
111 | ProjectWorkspace::Cargo { cargo, sysroot, rustc } | ||
112 | } | ||
113 | }; | ||
114 | |||
115 | Ok(res) | ||
116 | } | ||
117 | |||
118 | pub fn load_inline(project_json: ProjectJson) -> Result<ProjectWorkspace> { | ||
119 | let sysroot = match &project_json.sysroot_src { | ||
120 | Some(path) => Some(Sysroot::load(path)?), | ||
121 | None => None, | ||
122 | }; | ||
123 | |||
124 | Ok(ProjectWorkspace::Json { project: project_json, sysroot }) | ||
125 | } | ||
126 | |||
127 | /// Returns the roots for the current `ProjectWorkspace` | ||
128 | /// The return type contains the path and whether or not | ||
129 | /// the root is a member of the current workspace | ||
130 | pub fn to_roots(&self) -> Vec<PackageRoot> { | ||
131 | match self { | ||
132 | ProjectWorkspace::Json { project, sysroot } => project | ||
133 | .crates() | ||
134 | .map(|(_, krate)| PackageRoot { | ||
135 | is_member: krate.is_workspace_member, | ||
136 | include: krate.include.clone(), | ||
137 | exclude: krate.exclude.clone(), | ||
138 | }) | ||
139 | .collect::<FxHashSet<_>>() | ||
140 | .into_iter() | ||
141 | .chain(sysroot.as_ref().into_iter().flat_map(|sysroot| { | ||
142 | sysroot.crates().map(move |krate| PackageRoot { | ||
143 | is_member: false, | ||
144 | include: vec![sysroot[krate].root_dir().to_path_buf()], | ||
145 | exclude: Vec::new(), | ||
146 | }) | ||
147 | })) | ||
148 | .collect::<Vec<_>>(), | ||
149 | ProjectWorkspace::Cargo { cargo, sysroot, rustc } => cargo | ||
150 | .packages() | ||
151 | .map(|pkg| { | ||
152 | let is_member = cargo[pkg].is_member; | ||
153 | let pkg_root = cargo[pkg].root().to_path_buf(); | ||
154 | |||
155 | let mut include = vec![pkg_root.clone()]; | ||
156 | include.extend(cargo[pkg].out_dir.clone()); | ||
157 | |||
158 | let mut exclude = vec![pkg_root.join(".git")]; | ||
159 | if is_member { | ||
160 | exclude.push(pkg_root.join("target")); | ||
161 | } else { | ||
162 | exclude.push(pkg_root.join("tests")); | ||
163 | exclude.push(pkg_root.join("examples")); | ||
164 | exclude.push(pkg_root.join("benches")); | ||
165 | } | ||
166 | PackageRoot { is_member, include, exclude } | ||
167 | }) | ||
168 | .chain(sysroot.crates().map(|krate| PackageRoot { | ||
169 | is_member: false, | ||
170 | include: vec![sysroot[krate].root_dir().to_path_buf()], | ||
171 | exclude: Vec::new(), | ||
172 | })) | ||
173 | .chain(rustc.into_iter().flat_map(|rustc| { | ||
174 | rustc.packages().map(move |krate| PackageRoot { | ||
175 | is_member: false, | ||
176 | include: vec![rustc[krate].root().to_path_buf()], | ||
177 | exclude: Vec::new(), | ||
178 | }) | ||
179 | })) | ||
180 | .collect(), | ||
181 | } | ||
182 | } | ||
183 | |||
184 | pub fn n_packages(&self) -> usize { | ||
185 | match self { | ||
186 | ProjectWorkspace::Json { project, .. } => project.n_crates(), | ||
187 | ProjectWorkspace::Cargo { cargo, sysroot, rustc } => { | ||
188 | let rustc_package_len = rustc.as_ref().map_or(0, |rc| rc.packages().len()); | ||
189 | cargo.packages().len() + sysroot.crates().len() + rustc_package_len | ||
190 | } | ||
191 | } | ||
192 | } | ||
193 | |||
194 | pub fn to_crate_graph( | ||
195 | &self, | ||
196 | target: Option<&str>, | ||
197 | proc_macro_client: &ProcMacroClient, | ||
198 | load: &mut dyn FnMut(&AbsPath) -> Option<FileId>, | ||
199 | ) -> CrateGraph { | ||
200 | let mut crate_graph = match self { | ||
201 | ProjectWorkspace::Json { project, sysroot } => { | ||
202 | project_json_to_crate_graph(target, proc_macro_client, load, project, sysroot) | ||
203 | } | ||
204 | ProjectWorkspace::Cargo { cargo, sysroot, rustc } => { | ||
205 | cargo_to_crate_graph(target, proc_macro_client, load, cargo, sysroot, rustc) | ||
206 | } | ||
207 | }; | ||
208 | if crate_graph.patch_cfg_if() { | ||
209 | log::debug!("Patched std to depend on cfg-if") | ||
210 | } else { | ||
211 | log::debug!("Did not patch std to depend on cfg-if") | ||
212 | } | ||
213 | crate_graph | ||
214 | } | ||
215 | } | ||
216 | |||
217 | fn project_json_to_crate_graph( | ||
218 | target: Option<&str>, | ||
219 | proc_macro_client: &ProcMacroClient, | ||
220 | load: &mut dyn FnMut(&AbsPath) -> Option<FileId>, | ||
221 | project: &ProjectJson, | ||
222 | sysroot: &Option<Sysroot>, | ||
223 | ) -> CrateGraph { | ||
224 | let mut crate_graph = CrateGraph::default(); | ||
225 | let sysroot_deps = sysroot | ||
226 | .as_ref() | ||
227 | .map(|sysroot| sysroot_to_crate_graph(&mut crate_graph, sysroot, target, load)); | ||
228 | |||
229 | let mut cfg_cache: FxHashMap<Option<&str>, Vec<CfgFlag>> = FxHashMap::default(); | ||
230 | let crates: FxHashMap<CrateId, CrateId> = project | ||
231 | .crates() | ||
232 | .filter_map(|(crate_id, krate)| { | ||
233 | let file_path = &krate.root_module; | ||
234 | let file_id = load(&file_path)?; | ||
235 | Some((crate_id, krate, file_id)) | ||
236 | }) | ||
237 | .map(|(crate_id, krate, file_id)| { | ||
238 | let env = krate.env.clone().into_iter().collect(); | ||
239 | let proc_macro = | ||
240 | krate.proc_macro_dylib_path.clone().map(|it| proc_macro_client.by_dylib_path(&it)); | ||
241 | |||
242 | let target = krate.target.as_deref().or(target); | ||
243 | let target_cfgs = | ||
244 | cfg_cache.entry(target).or_insert_with(|| get_rustc_cfg_options(target)); | ||
245 | |||
246 | let mut cfg_options = CfgOptions::default(); | ||
247 | cfg_options.extend(target_cfgs.iter().chain(krate.cfg.iter()).cloned()); | ||
248 | ( | ||
249 | crate_id, | ||
250 | crate_graph.add_crate_root( | ||
251 | file_id, | ||
252 | krate.edition, | ||
253 | krate.display_name.clone(), | ||
254 | cfg_options, | ||
255 | env, | ||
256 | proc_macro.unwrap_or_default(), | ||
257 | ), | ||
258 | ) | ||
259 | }) | ||
260 | .collect(); | ||
261 | |||
262 | for (from, krate) in project.crates() { | ||
263 | if let Some(&from) = crates.get(&from) { | ||
264 | if let Some((public_deps, _proc_macro)) = &sysroot_deps { | ||
265 | for (name, to) in public_deps.iter() { | ||
266 | add_dep(&mut crate_graph, from, name.clone(), *to) | ||
267 | } | ||
268 | } | ||
269 | |||
270 | for dep in &krate.deps { | ||
271 | if let Some(&to) = crates.get(&dep.crate_id) { | ||
272 | add_dep(&mut crate_graph, from, dep.name.clone(), to) | ||
273 | } | ||
274 | } | ||
275 | } | ||
276 | } | ||
277 | crate_graph | ||
278 | } | ||
279 | |||
280 | fn cargo_to_crate_graph( | ||
281 | target: Option<&str>, | ||
282 | proc_macro_client: &ProcMacroClient, | ||
283 | load: &mut dyn FnMut(&AbsPath) -> Option<FileId>, | ||
284 | cargo: &CargoWorkspace, | ||
285 | sysroot: &Sysroot, | ||
286 | rustc: &Option<CargoWorkspace>, | ||
287 | ) -> CrateGraph { | ||
288 | let mut crate_graph = CrateGraph::default(); | ||
289 | let (public_deps, libproc_macro) = | ||
290 | sysroot_to_crate_graph(&mut crate_graph, sysroot, target, load); | ||
291 | |||
292 | let mut cfg_options = CfgOptions::default(); | ||
293 | cfg_options.extend(get_rustc_cfg_options(target)); | ||
294 | |||
295 | let mut pkg_to_lib_crate = FxHashMap::default(); | ||
296 | |||
297 | // Add test cfg for non-sysroot crates | ||
298 | cfg_options.insert_atom("test".into()); | ||
299 | cfg_options.insert_atom("debug_assertions".into()); | ||
300 | |||
301 | let mut pkg_crates = FxHashMap::default(); | ||
302 | |||
303 | // Next, create crates for each package, target pair | ||
304 | for pkg in cargo.packages() { | ||
305 | let mut lib_tgt = None; | ||
306 | for &tgt in cargo[pkg].targets.iter() { | ||
307 | if let Some(file_id) = load(&cargo[tgt].root) { | ||
308 | let crate_id = add_target_crate_root( | ||
309 | &mut crate_graph, | ||
310 | &cargo[pkg], | ||
311 | &cfg_options, | ||
312 | proc_macro_client, | ||
313 | file_id, | ||
314 | ); | ||
315 | if cargo[tgt].kind == TargetKind::Lib { | ||
316 | lib_tgt = Some((crate_id, cargo[tgt].name.clone())); | ||
317 | pkg_to_lib_crate.insert(pkg, crate_id); | ||
318 | } | ||
319 | if cargo[tgt].is_proc_macro { | ||
320 | if let Some(proc_macro) = libproc_macro { | ||
321 | add_dep( | ||
322 | &mut crate_graph, | ||
323 | crate_id, | ||
324 | CrateName::new("proc_macro").unwrap(), | ||
325 | proc_macro, | ||
326 | ); | ||
327 | } | ||
328 | } | ||
329 | |||
330 | pkg_crates.entry(pkg).or_insert_with(Vec::new).push(crate_id); | ||
331 | } | ||
332 | } | ||
333 | |||
334 | // Set deps to the core, std and to the lib target of the current package | ||
335 | for &from in pkg_crates.get(&pkg).into_iter().flatten() { | ||
336 | if let Some((to, name)) = lib_tgt.clone() { | ||
337 | if to != from { | ||
338 | // For root projects with dashes in their name, | ||
339 | // cargo metadata does not do any normalization, | ||
340 | // so we do it ourselves currently | ||
341 | let name = CrateName::normalize_dashes(&name); | ||
342 | add_dep(&mut crate_graph, from, name, to); | ||
343 | } | ||
344 | } | ||
345 | for (name, krate) in public_deps.iter() { | ||
346 | add_dep(&mut crate_graph, from, name.clone(), *krate); | ||
347 | } | ||
348 | } | ||
349 | } | ||
350 | |||
351 | // Now add a dep edge from all targets of upstream to the lib | ||
352 | // target of downstream. | ||
353 | for pkg in cargo.packages() { | ||
354 | for dep in cargo[pkg].dependencies.iter() { | ||
355 | let name = CrateName::new(&dep.name).unwrap(); | ||
356 | if let Some(&to) = pkg_to_lib_crate.get(&dep.pkg) { | ||
357 | for &from in pkg_crates.get(&pkg).into_iter().flatten() { | ||
358 | add_dep(&mut crate_graph, from, name.clone(), to) | ||
359 | } | ||
360 | } | ||
361 | } | ||
362 | } | ||
363 | |||
364 | let mut rustc_pkg_crates = FxHashMap::default(); | ||
365 | |||
366 | // If the user provided a path to rustc sources, we add all the rustc_private crates | ||
367 | // and create dependencies on them for the crates in the current workspace | ||
368 | if let Some(rustc_workspace) = rustc { | ||
369 | for pkg in rustc_workspace.packages() { | ||
370 | for &tgt in rustc_workspace[pkg].targets.iter() { | ||
371 | if rustc_workspace[tgt].kind != TargetKind::Lib { | ||
372 | continue; | ||
373 | } | ||
374 | // Exclude alloc / core / std | ||
375 | if rustc_workspace[tgt] | ||
376 | .root | ||
377 | .components() | ||
378 | .any(|c| c == Component::Normal("library".as_ref())) | ||
379 | { | ||
380 | continue; | ||
381 | } | ||
382 | |||
383 | if let Some(file_id) = load(&rustc_workspace[tgt].root) { | ||
384 | let crate_id = add_target_crate_root( | ||
385 | &mut crate_graph, | ||
386 | &rustc_workspace[pkg], | ||
387 | &cfg_options, | ||
388 | proc_macro_client, | ||
389 | file_id, | ||
390 | ); | ||
391 | pkg_to_lib_crate.insert(pkg, crate_id); | ||
392 | // Add dependencies on the core / std / alloc for rustc | ||
393 | for (name, krate) in public_deps.iter() { | ||
394 | add_dep(&mut crate_graph, crate_id, name.clone(), *krate); | ||
395 | } | ||
396 | rustc_pkg_crates.entry(pkg).or_insert_with(Vec::new).push(crate_id); | ||
397 | } | ||
398 | } | ||
399 | } | ||
400 | // Now add a dep edge from all targets of upstream to the lib | ||
401 | // target of downstream. | ||
402 | for pkg in rustc_workspace.packages() { | ||
403 | for dep in rustc_workspace[pkg].dependencies.iter() { | ||
404 | let name = CrateName::new(&dep.name).unwrap(); | ||
405 | if let Some(&to) = pkg_to_lib_crate.get(&dep.pkg) { | ||
406 | for &from in rustc_pkg_crates.get(&pkg).into_iter().flatten() { | ||
407 | add_dep(&mut crate_graph, from, name.clone(), to); | ||
408 | } | ||
409 | } | ||
410 | } | ||
411 | } | ||
412 | |||
413 | // Add dependencies for all the crates of the current workspace to rustc_private libraries | ||
414 | for dep in rustc_workspace.packages() { | ||
415 | let name = CrateName::normalize_dashes(&rustc_workspace[dep].name); | ||
416 | |||
417 | if let Some(&to) = pkg_to_lib_crate.get(&dep) { | ||
418 | for pkg in cargo.packages() { | ||
419 | if !cargo[pkg].is_member { | ||
420 | continue; | ||
421 | } | ||
422 | for &from in pkg_crates.get(&pkg).into_iter().flatten() { | ||
423 | add_dep(&mut crate_graph, from, name.clone(), to); | ||
424 | } | ||
425 | } | ||
426 | } | ||
427 | } | ||
428 | } | ||
429 | crate_graph | ||
430 | } | ||
431 | |||
432 | fn add_target_crate_root( | ||
433 | crate_graph: &mut CrateGraph, | ||
434 | pkg: &cargo_workspace::PackageData, | ||
435 | cfg_options: &CfgOptions, | ||
436 | proc_macro_client: &ProcMacroClient, | ||
437 | file_id: FileId, | ||
438 | ) -> CrateId { | ||
439 | let edition = pkg.edition; | ||
440 | let cfg_options = { | ||
441 | let mut opts = cfg_options.clone(); | ||
442 | for feature in pkg.features.iter() { | ||
443 | opts.insert_key_value("feature".into(), feature.into()); | ||
444 | } | ||
445 | opts.extend(pkg.cfgs.iter().cloned()); | ||
446 | opts | ||
447 | }; | ||
448 | let mut env = Env::default(); | ||
449 | if let Some(out_dir) = &pkg.out_dir { | ||
450 | // NOTE: cargo and rustc seem to hide non-UTF-8 strings from env! and option_env!() | ||
451 | if let Some(out_dir) = out_dir.to_str().map(|s| s.to_owned()) { | ||
452 | env.set("OUT_DIR", out_dir); | ||
453 | } | ||
454 | } | ||
455 | let proc_macro = pkg | ||
456 | .proc_macro_dylib_path | ||
457 | .as_ref() | ||
458 | .map(|it| proc_macro_client.by_dylib_path(&it)) | ||
459 | .unwrap_or_default(); | ||
460 | |||
461 | let display_name = CrateDisplayName::from_canonical_name(pkg.name.clone()); | ||
462 | let crate_id = crate_graph.add_crate_root( | ||
463 | file_id, | ||
464 | edition, | ||
465 | Some(display_name), | ||
466 | cfg_options, | ||
467 | env, | ||
468 | proc_macro.clone(), | ||
469 | ); | ||
470 | |||
471 | crate_id | ||
472 | } | ||
473 | |||
474 | fn sysroot_to_crate_graph( | ||
475 | crate_graph: &mut CrateGraph, | ||
476 | sysroot: &Sysroot, | ||
477 | target: Option<&str>, | ||
478 | load: &mut dyn FnMut(&AbsPath) -> Option<FileId>, | ||
479 | ) -> (Vec<(CrateName, CrateId)>, Option<CrateId>) { | ||
480 | let mut cfg_options = CfgOptions::default(); | ||
481 | cfg_options.extend(get_rustc_cfg_options(target)); | ||
482 | let sysroot_crates: FxHashMap<SysrootCrate, CrateId> = sysroot | ||
483 | .crates() | ||
484 | .filter_map(|krate| { | ||
485 | let file_id = load(&sysroot[krate].root)?; | ||
486 | |||
487 | let env = Env::default(); | ||
488 | let proc_macro = vec![]; | ||
489 | let display_name = CrateDisplayName::from_canonical_name(sysroot[krate].name.clone()); | ||
490 | let crate_id = crate_graph.add_crate_root( | ||
491 | file_id, | ||
492 | Edition::Edition2018, | ||
493 | Some(display_name), | ||
494 | cfg_options.clone(), | ||
495 | env, | ||
496 | proc_macro, | ||
497 | ); | ||
498 | Some((krate, crate_id)) | ||
499 | }) | ||
500 | .collect(); | ||
501 | |||
502 | for from in sysroot.crates() { | ||
503 | for &to in sysroot[from].deps.iter() { | ||
504 | let name = CrateName::new(&sysroot[to].name).unwrap(); | ||
505 | if let (Some(&from), Some(&to)) = (sysroot_crates.get(&from), sysroot_crates.get(&to)) { | ||
506 | add_dep(crate_graph, from, name, to); | ||
507 | } | ||
508 | } | ||
509 | } | ||
510 | |||
511 | let public_deps = sysroot | ||
512 | .public_deps() | ||
513 | .map(|(name, idx)| (CrateName::new(name).unwrap(), sysroot_crates[&idx])) | ||
514 | .collect::<Vec<_>>(); | ||
515 | |||
516 | let libproc_macro = sysroot.proc_macro().and_then(|it| sysroot_crates.get(&it).copied()); | ||
517 | (public_deps, libproc_macro) | ||
518 | } | ||
519 | |||
520 | fn get_rustc_cfg_options(target: Option<&str>) -> Vec<CfgFlag> { | ||
521 | let mut res = Vec::new(); | ||
522 | |||
523 | // Some nightly-only cfgs, which are required for stdlib | ||
524 | res.push(CfgFlag::Atom("target_thread_local".into())); | ||
525 | for &ty in ["8", "16", "32", "64", "cas", "ptr"].iter() { | ||
526 | for &key in ["target_has_atomic", "target_has_atomic_load_store"].iter() { | ||
527 | res.push(CfgFlag::KeyValue { key: key.to_string(), value: ty.into() }); | ||
528 | } | ||
529 | } | ||
530 | |||
531 | let rustc_cfgs = { | ||
532 | let mut cmd = Command::new(toolchain::rustc()); | ||
533 | cmd.args(&["--print", "cfg", "-O"]); | ||
534 | if let Some(target) = target { | ||
535 | cmd.args(&["--target", target]); | ||
536 | } | ||
537 | utf8_stdout(cmd) | ||
538 | }; | ||
539 | |||
540 | match rustc_cfgs { | ||
541 | Ok(rustc_cfgs) => res.extend(rustc_cfgs.lines().map(|it| it.parse().unwrap())), | ||
542 | Err(e) => log::error!("failed to get rustc cfgs: {:#}", e), | ||
543 | } | ||
544 | |||
545 | res | ||
546 | } | ||
547 | |||
548 | fn add_dep(graph: &mut CrateGraph, from: CrateId, name: CrateName, to: CrateId) { | ||
549 | if let Err(err) = graph.add_dep(from, name, to) { | ||
550 | log::error!("{}", err) | ||
551 | } | ||
552 | } | ||