1 // Copyright 2017 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 //! A thin wrapper around `Command` in the standard library which allows us to
12 //! read the arguments that are built up.
14 use std::ffi::{OsStr, OsString};
18 use std::process::{self, Output};
20 use rustc_back::LldFlavor;
26 env: Vec<(OsString, OsString)>,
32 CmdBatScript(OsString),
33 Lld(OsString, LldFlavor)
37 pub fn new<P: AsRef<OsStr>>(program: P) -> Command {
38 Command::_new(Program::Normal(program.as_ref().to_owned()))
41 pub fn bat_script<P: AsRef<OsStr>>(program: P) -> Command {
42 Command::_new(Program::CmdBatScript(program.as_ref().to_owned()))
45 pub fn lld<P: AsRef<OsStr>>(program: P, flavor: LldFlavor) -> Command {
46 Command::_new(Program::Lld(program.as_ref().to_owned(), flavor))
49 fn _new(program: Program) -> Command {
57 pub fn arg<P: AsRef<OsStr>>(&mut self, arg: P) -> &mut Command {
58 self._arg(arg.as_ref());
62 pub fn args<I>(&mut self, args: I) -> &mut Command
63 where I: IntoIterator,
64 I::Item: AsRef<OsStr>,
67 self._arg(arg.as_ref());
72 fn _arg(&mut self, arg: &OsStr) {
73 self.args.push(arg.to_owned());
76 pub fn env<K, V>(&mut self, key: K, value: V) -> &mut Command
77 where K: AsRef<OsStr>,
80 self._env(key.as_ref(), value.as_ref());
84 fn _env(&mut self, key: &OsStr, value: &OsStr) {
85 self.env.push((key.to_owned(), value.to_owned()));
88 pub fn output(&mut self) -> io::Result<Output> {
89 self.command().output()
92 pub fn command(&self) -> process::Command {
93 let mut ret = match self.program {
94 Program::Normal(ref p) => process::Command::new(p),
95 Program::CmdBatScript(ref p) => {
96 let mut c = process::Command::new("cmd");
100 Program::Lld(ref p, flavor) => {
101 let mut c = process::Command::new(p);
102 c.arg("-flavor").arg(match flavor {
103 LldFlavor::Wasm => "wasm",
104 LldFlavor::Ld => "gnu",
105 LldFlavor::Link => "link",
106 LldFlavor::Ld64 => "darwin",
111 ret.args(&self.args);
112 ret.envs(self.env.clone());
118 pub fn get_args(&self) -> &[OsString] {
122 pub fn take_args(&mut self) -> Vec<OsString> {
123 mem::replace(&mut self.args, Vec::new())
126 /// Returns a `true` if we're pretty sure that this'll blow OS spawn limits,
127 /// or `false` if we should attempt to spawn and see what the OS says.
128 pub fn very_likely_to_exceed_some_spawn_limit(&self) -> bool {
129 // We mostly only care about Windows in this method, on Unix the limits
130 // can be gargantuan anyway so we're pretty unlikely to hit them
135 // Ok so on Windows to spawn a process is 32,768 characters in its
136 // command line [1]. Unfortunately we don't actually have access to that
137 // as it's calculated just before spawning. Instead we perform a
138 // poor-man's guess as to how long our command line will be. We're
139 // assuming here that we don't have to escape every character...
141 // Turns out though that `cmd.exe` has even smaller limits, 8192
142 // characters [2]. Linkers can often be batch scripts (for example
143 // Emscripten, Gecko's current build system) which means that we're
144 // running through batch scripts. These linkers often just forward
145 // arguments elsewhere (and maybe tack on more), so if we blow 8192
146 // bytes we'll typically cause them to blow as well.
148 // Basically as a result just perform an inflated estimate of what our
149 // command line will look like and test if it's > 8192 (we actually
150 // test against 6k to artificially inflate our estimate). If all else
151 // fails we'll fall back to the normal unix logic of testing the OS
152 // error code if we fail to spawn and automatically re-spawning the
153 // linker with smaller arguments.
155 // [1]: https://msdn.microsoft.com/en-us/library/windows/desktop/ms682425(v=vs.85).aspx
156 // [2]: https://blogs.msdn.microsoft.com/oldnewthing/20031210-00/?p=41553
158 let estimated_command_line_len =
159 self.args.iter().map(|a| a.len()).sum::<usize>();
160 estimated_command_line_len > 1024 * 6
164 impl fmt::Debug for Command {
165 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
166 self.command().fmt(f)