|
| 1 | +//! A graph-like structure used to represent the rustc commands to build the project and the |
| 2 | +//! interdependencies between them. |
| 3 | +//! |
| 4 | +//! The BuildPlan structure is used to store the dependency graph of a dry run so that it can be |
| 5 | +//! shared with an external build system. Each Invocation in the BuildPlan comprises a single |
| 6 | +//! subprocess and defines the build environment, the outputs produced by the subprocess, and the |
| 7 | +//! dependencies on other Invocations. |
| 8 | +
|
| 9 | +use std::collections::BTreeMap; |
| 10 | + |
| 11 | +use core::TargetKind; |
| 12 | +use super::{Context, Kind, Unit}; |
| 13 | +use super::context::OutputFile; |
| 14 | +use util::{internal, CargoResult, ProcessBuilder}; |
| 15 | +use std::sync::Arc; |
| 16 | +use std::path::PathBuf; |
| 17 | +use serde_json; |
| 18 | +use semver; |
| 19 | + |
| 20 | +#[derive(Debug, Serialize)] |
| 21 | +struct Invocation { |
| 22 | + package_name: String, |
| 23 | + package_version: semver::Version, |
| 24 | + target_kind: TargetKind, |
| 25 | + kind: Kind, |
| 26 | + deps: Vec<usize>, |
| 27 | + outputs: Vec<PathBuf>, |
| 28 | + links: BTreeMap<PathBuf, PathBuf>, |
| 29 | + program: String, |
| 30 | + args: Vec<String>, |
| 31 | + env: BTreeMap<String, String>, |
| 32 | + cwd: Option<PathBuf>, |
| 33 | +} |
| 34 | + |
| 35 | +#[derive(Debug)] |
| 36 | +pub struct BuildPlan { |
| 37 | + invocation_map: BTreeMap<String, usize>, |
| 38 | + plan: SerializedBuildPlan, |
| 39 | +} |
| 40 | + |
| 41 | +#[derive(Debug, Serialize)] |
| 42 | +struct SerializedBuildPlan { |
| 43 | + invocations: Vec<Invocation>, |
| 44 | + inputs: Vec<PathBuf>, |
| 45 | +} |
| 46 | + |
| 47 | +impl Invocation { |
| 48 | + pub fn new(unit: &Unit, deps: Vec<usize>) -> Invocation { |
| 49 | + let id = unit.pkg.package_id(); |
| 50 | + Invocation { |
| 51 | + package_name: id.name().to_string(), |
| 52 | + package_version: id.version().clone(), |
| 53 | + kind: unit.kind, |
| 54 | + target_kind: unit.target.kind().clone(), |
| 55 | + deps: deps, |
| 56 | + outputs: Vec::new(), |
| 57 | + links: BTreeMap::new(), |
| 58 | + program: String::new(), |
| 59 | + args: Vec::new(), |
| 60 | + env: BTreeMap::new(), |
| 61 | + cwd: None, |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + pub fn add_output(&mut self, path: &PathBuf, link: &Option<PathBuf>) { |
| 66 | + self.outputs.push(path.clone()); |
| 67 | + if let Some(ref link) = *link { |
| 68 | + self.links.insert(link.clone(), path.clone()); |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + pub fn update_cmd(&mut self, cmd: ProcessBuilder) -> CargoResult<()> { |
| 73 | + self.program = cmd.get_program() |
| 74 | + .to_str() |
| 75 | + .ok_or_else(|| format_err!("unicode program string required"))? |
| 76 | + .to_string(); |
| 77 | + self.cwd = Some(cmd.get_cwd().unwrap().to_path_buf()); |
| 78 | + for arg in cmd.get_args().iter() { |
| 79 | + self.args.push( |
| 80 | + arg.to_str() |
| 81 | + .ok_or_else(|| format_err!("unicode argument string required"))? |
| 82 | + .to_string(), |
| 83 | + ); |
| 84 | + } |
| 85 | + for (var, value) in cmd.get_envs() { |
| 86 | + let value = match value { |
| 87 | + Some(s) => s, |
| 88 | + None => continue, |
| 89 | + }; |
| 90 | + self.env.insert( |
| 91 | + var.clone(), |
| 92 | + value |
| 93 | + .to_str() |
| 94 | + .ok_or_else(|| format_err!("unicode environment value required"))? |
| 95 | + .to_string(), |
| 96 | + ); |
| 97 | + } |
| 98 | + Ok(()) |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +impl BuildPlan { |
| 103 | + pub fn new() -> BuildPlan { |
| 104 | + BuildPlan { |
| 105 | + invocation_map: BTreeMap::new(), |
| 106 | + plan: SerializedBuildPlan::new(), |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + pub fn add(&mut self, cx: &Context, unit: &Unit) -> CargoResult<()> { |
| 111 | + let id = self.plan.invocations.len(); |
| 112 | + self.invocation_map.insert(unit.buildkey(), id); |
| 113 | + let deps = cx.dep_targets(&unit) |
| 114 | + .iter() |
| 115 | + .map(|dep| self.invocation_map[&dep.buildkey()]) |
| 116 | + .collect(); |
| 117 | + let invocation = Invocation::new(unit, deps); |
| 118 | + self.plan.invocations.push(invocation); |
| 119 | + Ok(()) |
| 120 | + } |
| 121 | + |
| 122 | + pub fn update( |
| 123 | + &mut self, |
| 124 | + invocation_name: String, |
| 125 | + cmd: ProcessBuilder, |
| 126 | + outputs: Arc<Vec<OutputFile>>, |
| 127 | + ) -> CargoResult<()> { |
| 128 | + let id = self.invocation_map[&invocation_name]; |
| 129 | + let invocation = self.plan |
| 130 | + .invocations |
| 131 | + .get_mut(id) |
| 132 | + .ok_or_else(|| internal(format!("couldn't find invocation for {}", invocation_name)))?; |
| 133 | + |
| 134 | + invocation.update_cmd(cmd)?; |
| 135 | + for output in outputs.iter() { |
| 136 | + invocation.add_output(&output.path, &output.hardlink); |
| 137 | + } |
| 138 | + |
| 139 | + Ok(()) |
| 140 | + } |
| 141 | + |
| 142 | + pub fn set_inputs(&mut self, inputs: Vec<PathBuf>) { |
| 143 | + self.plan.inputs = inputs; |
| 144 | + } |
| 145 | + |
| 146 | + pub fn output_plan(self) { |
| 147 | + let encoded = serde_json::to_string(&self.plan).unwrap(); |
| 148 | + println!("{}", encoded); |
| 149 | + } |
| 150 | +} |
| 151 | + |
| 152 | +impl SerializedBuildPlan { |
| 153 | + pub fn new() -> SerializedBuildPlan { |
| 154 | + SerializedBuildPlan { |
| 155 | + invocations: Vec::new(), |
| 156 | + inputs: Vec::new(), |
| 157 | + } |
| 158 | + } |
| 159 | +} |
0 commit comments