]> git.lizzy.rs Git - rust.git/blob - src/librustc_codegen_ssa/back/command.rs
Auto merge of #66129 - Nadrieril:refactor-slice-pat-usefulness, r=varkor
[rust.git] / src / librustc_codegen_ssa / back / command.rs
1 //! A thin wrapper around `Command` in the standard library which allows us to
2 //! read the arguments that are built up.
3
4 use std::ffi::{OsStr, OsString};
5 use std::fmt;
6 use std::io;
7 use std::mem;
8 use std::process::{self, Output};
9
10 use rustc_target::spec::LldFlavor;
11 use syntax::symbol::Symbol;
12
13 #[derive(Clone)]
14 pub struct Command {
15     program: Program,
16     args: Vec<OsString>,
17     env: Vec<(OsString, OsString)>,
18     env_remove: Vec<OsString>,
19 }
20
21 #[derive(Clone)]
22 enum Program {
23     Normal(OsString),
24     CmdBatScript(OsString),
25     Lld(OsString, LldFlavor)
26 }
27
28 impl Command {
29     pub fn new<P: AsRef<OsStr>>(program: P) -> Command {
30         Command::_new(Program::Normal(program.as_ref().to_owned()))
31     }
32
33     pub fn bat_script<P: AsRef<OsStr>>(program: P) -> Command {
34         Command::_new(Program::CmdBatScript(program.as_ref().to_owned()))
35     }
36
37     pub fn lld<P: AsRef<OsStr>>(program: P, flavor: LldFlavor) -> Command {
38         Command::_new(Program::Lld(program.as_ref().to_owned(), flavor))
39     }
40
41     fn _new(program: Program) -> Command {
42         Command {
43             program,
44             args: Vec::new(),
45             env: Vec::new(),
46             env_remove: Vec::new(),
47         }
48     }
49
50     pub fn arg<P: AsRef<OsStr>>(&mut self, arg: P) -> &mut Command {
51         self._arg(arg.as_ref());
52         self
53     }
54
55     pub fn sym_arg(&mut self, arg: Symbol) -> &mut Command {
56         self.arg(&*arg.as_str());
57         self
58     }
59
60     pub fn args<I>(&mut self, args: I) -> &mut Command
61     where
62         I: IntoIterator<Item: AsRef<OsStr>>,
63     {
64         for arg in args {
65             self._arg(arg.as_ref());
66         }
67         self
68     }
69
70     fn _arg(&mut self, arg: &OsStr) {
71         self.args.push(arg.to_owned());
72     }
73
74     pub fn env<K, V>(&mut self, key: K, value: V) -> &mut Command
75         where K: AsRef<OsStr>,
76               V: AsRef<OsStr>
77     {
78         self._env(key.as_ref(), value.as_ref());
79         self
80     }
81
82     fn _env(&mut self, key: &OsStr, value: &OsStr) {
83         self.env.push((key.to_owned(), value.to_owned()));
84     }
85
86     pub fn env_remove<K>(&mut self, key: K) -> &mut Command
87         where K: AsRef<OsStr>,
88     {
89         self._env_remove(key.as_ref());
90         self
91     }
92
93     fn _env_remove(&mut self, key: &OsStr) {
94         self.env_remove.push(key.to_owned());
95     }
96
97     pub fn output(&mut self) -> io::Result<Output> {
98         self.command().output()
99     }
100
101     pub fn command(&self) -> process::Command {
102         let mut ret = match self.program {
103             Program::Normal(ref p) => process::Command::new(p),
104             Program::CmdBatScript(ref p) => {
105                 let mut c = process::Command::new("cmd");
106                 c.arg("/c").arg(p);
107                 c
108             }
109             Program::Lld(ref p, flavor) => {
110                 let mut c = process::Command::new(p);
111                 c.arg("-flavor").arg(match flavor {
112                     LldFlavor::Wasm => "wasm",
113                     LldFlavor::Ld => "gnu",
114                     LldFlavor::Link => "link",
115                     LldFlavor::Ld64 => "darwin",
116                 });
117                 c
118             }
119         };
120         ret.args(&self.args);
121         ret.envs(self.env.clone());
122         for k in &self.env_remove {
123             ret.env_remove(k);
124         }
125         return ret
126     }
127
128     // extensions
129
130     pub fn get_args(&self) -> &[OsString] {
131         &self.args
132     }
133
134     pub fn take_args(&mut self) -> Vec<OsString> {
135         mem::take(&mut self.args)
136     }
137
138     /// Returns a `true` if we're pretty sure that this'll blow OS spawn limits,
139     /// or `false` if we should attempt to spawn and see what the OS says.
140     pub fn very_likely_to_exceed_some_spawn_limit(&self) -> bool {
141         // We mostly only care about Windows in this method, on Unix the limits
142         // can be gargantuan anyway so we're pretty unlikely to hit them
143         if cfg!(unix) {
144             return false
145         }
146
147         // Right now LLD doesn't support the `@` syntax of passing an argument
148         // through files, so regardless of the platform we try to go to the OS
149         // on this one.
150         if let Program::Lld(..) = self.program {
151             return false
152         }
153
154         // Ok so on Windows to spawn a process is 32,768 characters in its
155         // command line [1]. Unfortunately we don't actually have access to that
156         // as it's calculated just before spawning. Instead we perform a
157         // poor-man's guess as to how long our command line will be. We're
158         // assuming here that we don't have to escape every character...
159         //
160         // Turns out though that `cmd.exe` has even smaller limits, 8192
161         // characters [2]. Linkers can often be batch scripts (for example
162         // Emscripten, Gecko's current build system) which means that we're
163         // running through batch scripts. These linkers often just forward
164         // arguments elsewhere (and maybe tack on more), so if we blow 8192
165         // bytes we'll typically cause them to blow as well.
166         //
167         // Basically as a result just perform an inflated estimate of what our
168         // command line will look like and test if it's > 8192 (we actually
169         // test against 6k to artificially inflate our estimate). If all else
170         // fails we'll fall back to the normal unix logic of testing the OS
171         // error code if we fail to spawn and automatically re-spawning the
172         // linker with smaller arguments.
173         //
174         // [1]: https://msdn.microsoft.com/en-us/library/windows/desktop/ms682425(v=vs.85).aspx
175         // [2]: https://blogs.msdn.microsoft.com/oldnewthing/20031210-00/?p=41553
176
177         let estimated_command_line_len =
178             self.args.iter().map(|a| a.len()).sum::<usize>();
179         estimated_command_line_len > 1024 * 6
180     }
181 }
182
183 impl fmt::Debug for Command {
184     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
185         self.command().fmt(f)
186     }
187 }