1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272
use crate::cmd::{Command, MountKind, Runnable, SandboxBuilder}; use crate::prepare::Prepare; use crate::{Crate, Toolchain, Workspace}; use failure::Error; use remove_dir_all::remove_dir_all; use std::path::PathBuf; use std::vec::Vec; #[derive(Clone)] pub(crate) struct CratePatch { pub(crate) name: String, pub(crate) uri: String, pub(crate) branch: String, } /// Directory in the [`Workspace`](struct.Workspace.html) where builds can be executed. /// /// The build directory contains the source code of the crate being built and the target directory /// used by cargo to store build artifacts. If multiple builds are executed in the same build /// directory they will share the target directory. pub struct BuildDirectory { workspace: Workspace, name: String, } /// Builder for configuring builds in a [`BuildDirectory`](struct.BuildDirectory.html). pub struct BuildBuilder<'a> { build_dir: &'a mut BuildDirectory, toolchain: &'a Toolchain, krate: &'a Crate, sandbox: SandboxBuilder, patches: Vec<CratePatch>, } impl<'a> BuildBuilder<'a> { /// Add a patch to this build. /// Patches get added to the crate's Cargo.toml in the `patch.crates-io` table. /// # Example /// /// ```no_run /// # use rustwide::{WorkspaceBuilder, Toolchain, Crate, cmd::SandboxBuilder}; /// # use std::error::Error; /// # fn main() -> Result<(), Box<dyn Error>> { /// # let workspace = WorkspaceBuilder::new("".as_ref(), "").init()?; /// # let toolchain = Toolchain::dist(""); /// # let krate = Crate::local("".as_ref()); /// # let sandbox = SandboxBuilder::new(); /// let mut build_dir = workspace.build_dir("foo"); /// build_dir.build(&toolchain, &krate, sandbox) /// .patch_with_git("bar", "https://github.com/foo/bar", "baz") /// .run(|build| { /// build.cargo().args(&["test", "--all"]).run()?; /// Ok(()) /// })?; /// # Ok(()) /// # } pub fn patch_with_git(mut self, name: &str, uri: &str, branch: &str) -> Self { self.patches.push(CratePatch { name: name.into(), uri: uri.into(), branch: branch.into(), }); self } /// Run a sandboxed build of the provided crate with the provided toolchain. The closure will /// be provided an instance of [`Build`](struct.Build.html) that allows spawning new processes /// inside the sandbox. /// /// All the state will be kept on disk as long as the closure doesn't exit: after that things /// might be removed. /// # Example /// /// ```no_run /// # use rustwide::{WorkspaceBuilder, Toolchain, Crate, cmd::SandboxBuilder}; /// # use std::error::Error; /// # fn main() -> Result<(), Box<dyn Error>> { /// # let workspace = WorkspaceBuilder::new("".as_ref(), "").init()?; /// # let toolchain = Toolchain::dist(""); /// # let krate = Crate::local("".as_ref()); /// # let sandbox = SandboxBuilder::new(); /// let mut build_dir = workspace.build_dir("foo"); /// build_dir.build(&toolchain, &krate, sandbox).run(|build| { /// build.cargo().args(&["test", "--all"]).run()?; /// Ok(()) /// })?; /// # Ok(()) /// # } pub fn run<R, F: FnOnce(&Build) -> Result<R, Error>>(self, f: F) -> Result<R, Error> { self.build_dir .run(self.toolchain, self.krate, self.sandbox, self.patches, f) } } impl BuildDirectory { pub(crate) fn new(workspace: Workspace, name: &str) -> Self { Self { workspace, name: name.into(), } } /// Create a build in this build directory. Returns a builder that can be used /// to configure the build and run it. /// /// # Example /// /// ```no_run /// # use rustwide::{WorkspaceBuilder, Toolchain, Crate, cmd::SandboxBuilder}; /// # use std::error::Error; /// # fn main() -> Result<(), Box<dyn Error>> { /// # let workspace = WorkspaceBuilder::new("".as_ref(), "").init()?; /// # let toolchain = Toolchain::dist(""); /// # let krate = Crate::local("".as_ref()); /// # let sandbox = SandboxBuilder::new(); /// let mut build_dir = workspace.build_dir("foo"); /// build_dir.build(&toolchain, &krate, sandbox).run(|build| { /// build.cargo().args(&["test", "--all"]).run()?; /// Ok(()) /// })?; /// # Ok(()) /// # } /// ``` pub fn build<'a>( &'a mut self, toolchain: &'a Toolchain, krate: &'a Crate, sandbox: SandboxBuilder, ) -> BuildBuilder { BuildBuilder { build_dir: self, toolchain, krate, sandbox, patches: Vec::new(), } } pub(crate) fn run<R, F: FnOnce(&Build) -> Result<R, Error>>( &mut self, toolchain: &Toolchain, krate: &Crate, sandbox: SandboxBuilder, patches: Vec<CratePatch>, f: F, ) -> Result<R, Error> { let source_dir = self.source_dir(); if source_dir.exists() { remove_dir_all(&source_dir)?; } let mut prepare = Prepare::new(&self.workspace, toolchain, krate, &source_dir, patches); prepare.prepare()?; std::fs::create_dir_all(self.target_dir())?; let res = f(&Build { dir: self, toolchain, sandbox, })?; remove_dir_all(&source_dir)?; Ok(res) } /// Remove all the contents of the build directory, freeing disk space. pub fn purge(&mut self) -> Result<(), Error> { let build_dir = self.build_dir(); if build_dir.exists() { remove_dir_all(build_dir)?; } Ok(()) } fn build_dir(&self) -> PathBuf { self.workspace.builds_dir().join(&self.name) } fn source_dir(&self) -> PathBuf { self.build_dir().join("source") } fn target_dir(&self) -> PathBuf { self.build_dir().join("target") } } /// API to interact with a running build. /// /// This is created from [`BuildDirectory::build`](struct.BuildDirectory.html#method.build) pub struct Build<'ws> { dir: &'ws BuildDirectory, toolchain: &'ws Toolchain, sandbox: SandboxBuilder, } impl<'ws> Build<'ws> { /// Run a command inside the sandbox. /// /// Any `cargo` invocation will automatically be configured to use a target directory mounted /// outside the sandbox. The crate's source directory will be the working directory for the /// command. /// /// # Example /// /// ```no_run /// # use rustwide::{WorkspaceBuilder, Toolchain, Crate, cmd::SandboxBuilder}; /// # use std::error::Error; /// # fn main() -> Result<(), Box<dyn Error>> { /// # let workspace = WorkspaceBuilder::new("".as_ref(), "").init()?; /// # let toolchain = Toolchain::dist(""); /// # let krate = Crate::local("".as_ref()); /// # let sandbox = SandboxBuilder::new(); /// let mut build_dir = workspace.build_dir("foo"); /// build_dir.build(&toolchain, &krate, sandbox).run(|build| { /// build.cmd("rustfmt").args(&["--check", "src/main.rs"]).run()?; /// Ok(()) /// })?; /// # Ok(()) /// # } /// ``` pub fn cmd<'pl, R: Runnable>(&self, bin: R) -> Command<'ws, 'pl> { let container_dir = &*crate::cmd::container_dirs::TARGET_DIR; Command::new_sandboxed( &self.dir.workspace, self.sandbox .clone() .mount(&self.dir.target_dir(), container_dir, MountKind::ReadWrite), bin, ) .cd(self.dir.source_dir()) .env("CARGO_TARGET_DIR", container_dir) } /// Run `cargo` inside the sandbox, using the toolchain chosen for the build. /// /// `cargo` will automatically be configured to use a target directory mounted outside the /// sandbox. The crate's source directory will be the working directory for the command. /// /// # Example /// /// ```no_run /// # use rustwide::{WorkspaceBuilder, Toolchain, Crate, cmd::SandboxBuilder}; /// # use std::error::Error; /// # fn main() -> Result<(), Box<dyn Error>> { /// # let workspace = WorkspaceBuilder::new("".as_ref(), "").init()?; /// # let toolchain = Toolchain::dist(""); /// # let krate = Crate::local("".as_ref()); /// # let sandbox = SandboxBuilder::new(); /// let mut build_dir = workspace.build_dir("foo"); /// build_dir.build(&toolchain, &krate, sandbox).run(|build| { /// build.cargo().args(&["test", "--all"]).run()?; /// Ok(()) /// })?; /// # Ok(()) /// # } /// ``` pub fn cargo<'pl>(&self) -> Command<'ws, 'pl> { self.cmd(self.toolchain.cargo()) } /// Get the path to the source code on the host machine (outside the sandbox). pub fn host_source_dir(&self) -> PathBuf { self.dir.source_dir() } /// Get the path to the target directory on the host machine (outside the sandbox). pub fn host_target_dir(&self) -> PathBuf { self.dir.target_dir() } }