]> git.lizzy.rs Git - rust.git/blob - src/librustc_trans/back/command.rs
Rollup merge of #48698 - ishitatsuyuki:burn-equate, r=nikomatsakis
[rust.git] / src / librustc_trans / back / command.rs
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.
4 //
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.
10
11 //! A thin wrapper around `Command` in the standard library which allows us to
12 //! read the arguments that are built up.
13
14 use std::ffi::{OsStr, OsString};
15 use std::fmt;
16 use std::io;
17 use std::mem;
18 use std::process::{self, Output};
19
20 use rustc_back::LldFlavor;
21
22 #[derive(Clone)]
23 pub struct Command {
24     program: Program,
25     args: Vec<OsString>,
26     env: Vec<(OsString, OsString)>,
27 }
28
29 #[derive(Clone)]
30 enum Program {
31     Normal(OsString),
32     CmdBatScript(OsString),
33     Lld(OsString, LldFlavor)
34 }
35
36 impl Command {
37     pub fn new<P: AsRef<OsStr>>(program: P) -> Command {
38         Command::_new(Program::Normal(program.as_ref().to_owned()))
39     }
40
41     pub fn bat_script<P: AsRef<OsStr>>(program: P) -> Command {
42         Command::_new(Program::CmdBatScript(program.as_ref().to_owned()))
43     }
44
45     pub fn lld<P: AsRef<OsStr>>(program: P, flavor: LldFlavor) -> Command {
46         Command::_new(Program::Lld(program.as_ref().to_owned(), flavor))
47     }
48
49     fn _new(program: Program) -> Command {
50         Command {
51             program,
52             args: Vec::new(),
53             env: Vec::new(),
54         }
55     }
56
57     pub fn arg<P: AsRef<OsStr>>(&mut self, arg: P) -> &mut Command {
58         self._arg(arg.as_ref());
59         self
60     }
61
62     pub fn args<I>(&mut self, args: I) -> &mut Command
63         where I: IntoIterator,
64               I::Item: AsRef<OsStr>,
65     {
66         for arg in args {
67             self._arg(arg.as_ref());
68         }
69         self
70     }
71
72     fn _arg(&mut self, arg: &OsStr) {
73         self.args.push(arg.to_owned());
74     }
75
76     pub fn env<K, V>(&mut self, key: K, value: V) -> &mut Command
77         where K: AsRef<OsStr>,
78               V: AsRef<OsStr>
79     {
80         self._env(key.as_ref(), value.as_ref());
81         self
82     }
83
84     fn _env(&mut self, key: &OsStr, value: &OsStr) {
85         self.env.push((key.to_owned(), value.to_owned()));
86     }
87
88     pub fn output(&mut self) -> io::Result<Output> {
89         self.command().output()
90     }
91
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");
97                 c.arg("/c").arg(p);
98                 c
99             }
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",
107                 });
108                 c
109             }
110         };
111         ret.args(&self.args);
112         ret.envs(self.env.clone());
113         return ret
114     }
115
116     // extensions
117
118     pub fn get_args(&self) -> &[OsString] {
119         &self.args
120     }
121
122     pub fn take_args(&mut self) -> Vec<OsString> {
123         mem::replace(&mut self.args, Vec::new())
124     }
125
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
131         if cfg!(unix) {
132             return false
133         }
134
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...
140         //
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.
147         //
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.
154         //
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
157
158         let estimated_command_line_len =
159             self.args.iter().map(|a| a.len()).sum::<usize>();
160         estimated_command_line_len > 1024 * 6
161     }
162 }
163
164 impl fmt::Debug for Command {
165     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
166         self.command().fmt(f)
167     }
168 }