Skip to content

Commit 7da5c84

Browse files
committed
Auto merge of #4546 - derekdreery:add_docs, r=matklad
Add some docs I was reading around the source, trying to understand it, and writing docs to help myself. I thought they may be useful for the main crate.
2 parents a92b7c8 + 55f56e7 commit 7da5c84

File tree

4 files changed

+74
-4
lines changed

4 files changed

+74
-4
lines changed

src/cargo/ops/cargo_rustc/context.rs

+5-4
Original file line numberDiff line numberDiff line change
@@ -561,7 +561,7 @@ impl<'a, 'cfg> Context<'a, 'cfg> {
561561
/// and link_stem would be out_dir/foo
562562
/// This function returns it in two parts so the caller can add prefix/suffix
563563
/// to filename separately
564-
564+
///
565565
/// Returns an Option because in some cases we don't want to link
566566
/// (eg a dependent lib)
567567
pub fn link_stem(&mut self, unit: &Unit<'a>) -> Option<(PathBuf, String)> {
@@ -595,9 +595,10 @@ impl<'a, 'cfg> Context<'a, 'cfg> {
595595

596596
/// Return the filenames that the given target for the given profile will
597597
/// generate as a list of 3-tuples (filename, link_dst, linkable)
598-
/// filename: filename rustc compiles to. (Often has metadata suffix).
599-
/// link_dst: Optional file to link/copy the result to (without metadata suffix)
600-
/// linkable: Whether possible to link against file (eg it's a library)
598+
///
599+
/// - filename: filename rustc compiles to. (Often has metadata suffix).
600+
/// - link_dst: Optional file to link/copy the result to (without metadata suffix)
601+
/// - linkable: Whether possible to link against file (eg it's a library)
601602
pub fn target_filenames(&mut self, unit: &Unit<'a>)
602603
-> CargoResult<Arc<Vec<(PathBuf, Option<PathBuf>, bool)>>> {
603604
if let Some(cache) = self.target_filenames.get(unit) {

src/cargo/ops/cargo_rustc/custom_build.rs

+2
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,10 @@ pub struct BuildOutput {
3434
pub warnings: Vec<String>,
3535
}
3636

37+
/// Map of packages to build info
3738
pub type BuildMap = HashMap<(PackageId, Kind), BuildOutput>;
3839

40+
/// Build info and overrides
3941
pub struct BuildState {
4042
pub outputs: Mutex<BuildMap>,
4143
overrides: HashMap<(String, Kind), BuildOutput>,

src/cargo/ops/cargo_rustc/mod.rs

+22
Original file line numberDiff line numberDiff line change
@@ -37,26 +37,48 @@ mod layout;
3737
mod links;
3838
mod output_depinfo;
3939

40+
/// Whether an object is for the host arch, or the target arch.
41+
///
42+
/// These will be the same unless cross-compiling.
4043
#[derive(PartialEq, Eq, Hash, Debug, Clone, Copy, PartialOrd, Ord)]
4144
pub enum Kind { Host, Target }
4245

46+
/// Configuration information for a rustc build.
4347
#[derive(Default, Clone)]
4448
pub struct BuildConfig {
49+
/// The host arch triple
50+
///
51+
/// e.g. x86_64-unknown-linux-gnu, would be
52+
/// - machine: x86_64
53+
/// - hardware-platform: unknown
54+
/// - operating system: linux-gnu
4555
pub host_triple: String,
56+
/// Build information for the host arch
4657
pub host: TargetConfig,
58+
/// The target arch triple, defaults to host arch
4759
pub requested_target: Option<String>,
60+
/// Build information for the target
4861
pub target: TargetConfig,
62+
/// How many rustc jobs to run in parallel
4963
pub jobs: u32,
64+
/// Whether we are building for release
5065
pub release: bool,
66+
/// Whether we are running tests
5167
pub test: bool,
68+
/// Whether we are building documentation
5269
pub doc_all: bool,
70+
/// Whether to print std output in json format (for machine reading)
5371
pub json_messages: bool,
5472
}
5573

74+
/// Information required to build for a target
5675
#[derive(Clone, Default)]
5776
pub struct TargetConfig {
77+
/// The path of archiver (lib builder) for this target.
5878
pub ar: Option<PathBuf>,
79+
/// The path of the linker for this target.
5980
pub linker: Option<PathBuf>,
81+
/// Special build options for any necessary input files (filename -> options)
6082
pub overrides: HashMap<String, BuildOutput>,
6183
}
6284

src/cargo/util/process_builder.rs

+45
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,21 @@ use shell_escape::escape;
1111
use util::{CargoResult, CargoResultExt, CargoError, process_error, read2};
1212
use util::errors::CargoErrorKind;
1313

14+
/// A builder object for an external process, similar to `std::process::Command`.
1415
#[derive(Clone, Debug)]
1516
pub struct ProcessBuilder {
17+
/// The program to execute.
1618
program: OsString,
19+
/// A list of arguments to pass to the program.
1720
args: Vec<OsString>,
21+
/// Any environment variables that should be set for the program.
1822
env: HashMap<String, Option<OsString>>,
23+
/// Which directory to run the program from.
1924
cwd: Option<OsString>,
25+
/// The `make` jobserver. See the [jobserver crate][jobserver_docs] for
26+
/// more information.
27+
///
28+
/// [jobserver_docs]: https://docs.rs/jobserver/0.1.6/jobserver/
2029
jobserver: Option<Client>,
2130
}
2231

@@ -33,70 +42,89 @@ impl fmt::Display for ProcessBuilder {
3342
}
3443

3544
impl ProcessBuilder {
45+
/// (chainable) Set the executable for the process.
3646
pub fn program<T: AsRef<OsStr>>(&mut self, program: T) -> &mut ProcessBuilder {
3747
self.program = program.as_ref().to_os_string();
3848
self
3949
}
4050

51+
/// (chainable) Add an arg to the args list.
4152
pub fn arg<T: AsRef<OsStr>>(&mut self, arg: T) -> &mut ProcessBuilder {
4253
self.args.push(arg.as_ref().to_os_string());
4354
self
4455
}
4556

57+
/// (chainable) Add many args to the args list.
4658
pub fn args<T: AsRef<OsStr>>(&mut self, arguments: &[T]) -> &mut ProcessBuilder {
4759
self.args.extend(arguments.iter().map(|t| {
4860
t.as_ref().to_os_string()
4961
}));
5062
self
5163
}
5264

65+
/// (chainable) Replace args with new args list
5366
pub fn args_replace<T: AsRef<OsStr>>(&mut self, arguments: &[T]) -> &mut ProcessBuilder {
5467
self.args = arguments.iter().map(|t| {
5568
t.as_ref().to_os_string()
5669
}).collect();
5770
self
5871
}
5972

73+
/// (chainable) Set the current working directory of the process
6074
pub fn cwd<T: AsRef<OsStr>>(&mut self, path: T) -> &mut ProcessBuilder {
6175
self.cwd = Some(path.as_ref().to_os_string());
6276
self
6377
}
6478

79+
/// (chainable) Set an environment variable for the process.
6580
pub fn env<T: AsRef<OsStr>>(&mut self, key: &str,
6681
val: T) -> &mut ProcessBuilder {
6782
self.env.insert(key.to_string(), Some(val.as_ref().to_os_string()));
6883
self
6984
}
7085

86+
/// (chainable) Unset an environment variable for the process.
7187
pub fn env_remove(&mut self, key: &str) -> &mut ProcessBuilder {
7288
self.env.insert(key.to_string(), None);
7389
self
7490
}
7591

92+
/// Get the executable name.
7693
pub fn get_program(&self) -> &OsString {
7794
&self.program
7895
}
7996

97+
/// Get the program arguments
8098
pub fn get_args(&self) -> &[OsString] {
8199
&self.args
82100
}
83101

102+
/// Get the current working directory for the process
84103
pub fn get_cwd(&self) -> Option<&Path> {
85104
self.cwd.as_ref().map(Path::new)
86105
}
87106

107+
/// Get an environment variable as the process will see it (will inherit from environment
108+
/// unless explicitally unset).
88109
pub fn get_env(&self, var: &str) -> Option<OsString> {
89110
self.env.get(var).cloned().or_else(|| Some(env::var_os(var)))
90111
.and_then(|s| s)
91112
}
92113

114+
/// Get all environment variables explicitally set or unset for the process (not inherited
115+
/// vars).
93116
pub fn get_envs(&self) -> &HashMap<String, Option<OsString>> { &self.env }
94117

118+
/// Set the `make` jobserver. See the [jobserver crate][jobserver_docs] for
119+
/// more information.
120+
///
121+
/// [jobserver_docs]: https://docs.rs/jobserver/0.1.6/jobserver/
95122
pub fn inherit_jobserver(&mut self, jobserver: &Client) -> &mut Self {
96123
self.jobserver = Some(jobserver.clone());
97124
self
98125
}
99126

127+
/// Run the process, waiting for completion, and mapping non-success exit codes to an error.
100128
pub fn exec(&self) -> CargoResult<()> {
101129
let mut command = self.build_command();
102130
let exit = command.status().chain_err(|| {
@@ -114,6 +142,9 @@ impl ProcessBuilder {
114142
}
115143
}
116144

145+
/// On unix, executes the process using the unix syscall `execvp`, which will block this
146+
/// process, and will only return if there is an error. On windows this is a synonym for
147+
/// `exec`.
117148
#[cfg(unix)]
118149
pub fn exec_replace(&self) -> CargoResult<()> {
119150
use std::os::unix::process::CommandExt;
@@ -125,11 +156,15 @@ impl ProcessBuilder {
125156
&format!("could not execute process `{}`", self.debug_string()), None, None))))
126157
}
127158

159+
/// On unix, executes the process using the unix syscall `execvp`, which will block this
160+
/// process, and will only return if there is an error. On windows this is a synonym for
161+
/// `exec`.
128162
#[cfg(windows)]
129163
pub fn exec_replace(&self) -> CargoResult<()> {
130164
self.exec()
131165
}
132166

167+
/// Execute the process, returning the stdio output, or an error if non-zero exit status.
133168
pub fn exec_with_output(&self) -> CargoResult<Output> {
134169
let mut command = self.build_command();
135170

@@ -149,6 +184,12 @@ impl ProcessBuilder {
149184
}
150185
}
151186

187+
/// Execute a command, passing each line of stdout and stderr to the supplied callbacks, which
188+
/// can mutate the string data.
189+
///
190+
/// If any invocations of these function return an error, it will be propagated.
191+
///
192+
/// Optionally, output can be passed to errors using `print_output`
152193
pub fn exec_with_streaming(&self,
153194
on_stdout_line: &mut FnMut(&str) -> CargoResult<()>,
154195
on_stderr_line: &mut FnMut(&str) -> CargoResult<()>,
@@ -226,6 +267,8 @@ impl ProcessBuilder {
226267
Ok(output)
227268
}
228269

270+
/// Converts ProcessBuilder into a `std::process::Command`, and handles the jobserver if
271+
/// present.
229272
pub fn build_command(&self) -> Command {
230273
let mut command = Command::new(&self.program);
231274
if let Some(cwd) = self.get_cwd() {
@@ -246,6 +289,7 @@ impl ProcessBuilder {
246289
command
247290
}
248291

292+
/// Get the command line for the process as a string.
249293
fn debug_string(&self) -> String {
250294
let mut program = format!("{}", self.program.to_string_lossy());
251295
for arg in &self.args {
@@ -256,6 +300,7 @@ impl ProcessBuilder {
256300
}
257301
}
258302

303+
/// A helper function to create a ProcessBuilder.
259304
pub fn process<T: AsRef<OsStr>>(cmd: T) -> ProcessBuilder {
260305
ProcessBuilder {
261306
program: cmd.as_ref().to_os_string(),

0 commit comments

Comments
 (0)