run_make_support/external_deps/
rustdoc.rs

1use std::ffi::OsStr;
2use std::path::Path;
3
4use crate::command::Command;
5use crate::env::{env_var, env_var_os};
6use crate::util::set_host_rpath;
7
8/// Construct a plain `rustdoc` invocation with no flags set.
9#[track_caller]
10pub fn bare_rustdoc() -> Rustdoc {
11    Rustdoc::bare()
12}
13
14/// Construct a new `rustdoc` invocation with `-L $(TARGET_RPATH_DIR)` set.
15#[track_caller]
16pub fn rustdoc() -> Rustdoc {
17    Rustdoc::new()
18}
19
20#[derive(Debug)]
21#[must_use]
22pub struct Rustdoc {
23    cmd: Command,
24}
25
26crate::macros::impl_common_helpers!(Rustdoc);
27
28#[track_caller]
29fn setup_common() -> Command {
30    let rustdoc = env_var("RUSTDOC");
31    let mut cmd = Command::new(rustdoc);
32    set_host_rpath(&mut cmd);
33    cmd
34}
35
36impl Rustdoc {
37    /// Construct a bare `rustdoc` invocation.
38    #[track_caller]
39    pub fn bare() -> Self {
40        let cmd = setup_common();
41        Self { cmd }
42    }
43
44    /// Construct a `rustdoc` invocation with `-L $(TARGET_RPATH_DIR)` set.
45    #[track_caller]
46    pub fn new() -> Self {
47        let mut cmd = setup_common();
48        cmd.arg("-L").arg(env_var_os("TARGET_RPATH_DIR"));
49        Self { cmd }
50    }
51
52    /// Specify where an external library is located.
53    pub fn extern_<P: AsRef<Path>>(&mut self, crate_name: &str, path: P) -> &mut Self {
54        assert!(
55            !crate_name.contains(|c: char| c.is_whitespace() || c == '\\' || c == '/'),
56            "crate name cannot contain whitespace or path separators"
57        );
58
59        let path = path.as_ref().to_string_lossy();
60
61        self.cmd.arg("--extern");
62        self.cmd.arg(format!("{crate_name}={path}"));
63
64        self
65    }
66
67    /// Specify path to the input file.
68    pub fn input<P: AsRef<Path>>(&mut self, path: P) -> &mut Self {
69        self.cmd.arg(path.as_ref());
70        self
71    }
72
73    /// Specify output directory.
74    #[doc(alias = "output")]
75    pub fn out_dir<P: AsRef<Path>>(&mut self, path: P) -> &mut Self {
76        self.cmd.arg("--out-dir").arg(path.as_ref());
77        self
78    }
79
80    /// Given a `path`, pass `@{path}` to `rustdoc` as an
81    /// [arg file](https://doc.rust-lang.org/rustdoc/command-line-arguments.html#path-load-command-line-flags-from-a-path).
82    pub fn arg_file<P: AsRef<Path>>(&mut self, path: P) -> &mut Self {
83        self.cmd.arg(format!("@{}", path.as_ref().display()));
84        self
85    }
86
87    /// Specify a stdin input buffer.
88    pub fn stdin_buf<I: AsRef<[u8]>>(&mut self, input: I) -> &mut Self {
89        self.cmd.stdin_buf(input);
90        self
91    }
92
93    /// Specify the edition year.
94    pub fn edition(&mut self, edition: &str) -> &mut Self {
95        self.cmd.arg("--edition");
96        self.cmd.arg(edition);
97        self
98    }
99
100    /// Specify the target triple, or a path to a custom target json spec file.
101    pub fn target<S: AsRef<str>>(&mut self, target: S) -> &mut Self {
102        let target = target.as_ref();
103        self.cmd.arg(format!("--target={target}"));
104        self
105    }
106
107    /// Specify the crate type.
108    pub fn crate_type(&mut self, crate_type: &str) -> &mut Self {
109        self.cmd.arg("--crate-type");
110        self.cmd.arg(crate_type);
111        self
112    }
113
114    /// Specify the crate name.
115    pub fn crate_name<S: AsRef<OsStr>>(&mut self, name: S) -> &mut Self {
116        self.cmd.arg("--crate-name");
117        self.cmd.arg(name.as_ref());
118        self
119    }
120
121    /// Add a directory to the library search path. It corresponds to the `-L`
122    /// rustdoc option.
123    pub fn library_search_path<P: AsRef<Path>>(&mut self, path: P) -> &mut Self {
124        self.cmd.arg("-L");
125        self.cmd.arg(path.as_ref());
126        self
127    }
128
129    /// Specify the output format.
130    pub fn output_format(&mut self, format: &str) -> &mut Self {
131        self.cmd.arg("--output-format");
132        self.cmd.arg(format);
133        self
134    }
135}