Skip to content

Commit 4545379

Browse files
aidanhsluser
authored andcommitted
Expose direct access to toolchain packagers
1 parent 88dd7aa commit 4545379

File tree

5 files changed

+60
-15
lines changed

5 files changed

+60
-15
lines changed

src/cmdline.rs

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,16 @@ arg_enum!{
3636
pub enum Command {
3737
/// Show cache statistics and exit.
3838
ShowStats(StatsFormat),
39-
/// Zero cache statistics and exit.
40-
ZeroStats,
4139
/// Run background server.
4240
InternalStartServer,
4341
/// Start background server as a subprocess.
4442
StartServer,
4543
/// Stop background server.
4644
StopServer,
45+
/// Zero cache statistics and exit.
46+
ZeroStats,
47+
/// Package a toolchain for distributed compilation (executable, out)
48+
PackageToolchain(PathBuf, PathBuf),
4749
/// Run a compiler command.
4850
Compile {
4951
/// The binary to execute.
@@ -71,11 +73,13 @@ pub fn get_app<'a, 'b>() -> App<'a, 'b> {
7173
" Azure: ", cfg!(feature = "azure"), "\n")
7274
)
7375
.args_from_usage(
74-
"-s --show-stats 'show cache statistics'
75-
-z, --zero-stats 'zero statistics counters'
76-
--start-server 'start background server'
77-
--stop-server 'stop background server'"
76+
"-s --show-stats 'show cache statistics'
77+
--start-server 'start background server'
78+
--stop-server 'stop background server'
79+
-z, --zero-stats 'zero statistics counters'"
7880
)
81+
.arg(Arg::from_usage("--package-toolchain <executable> <out> 'package toolchain for distributed compilation'")
82+
.required(false))
7983
.arg(Arg::from_usage("--stats-format 'set output format of statistics'")
8084
.possible_values(&StatsFormat::variants())
8185
.default_value("text"))
@@ -131,6 +135,7 @@ pub fn parse() -> Result<Command> {
131135
let start_server = matches.is_present("start-server");
132136
let stop_server = matches.is_present("stop-server");
133137
let zero_stats = matches.is_present("zero-stats");
138+
let package_toolchain = matches.is_present("package-toolchain");
134139
let cmd = matches.values_of_os("cmd");
135140
// Ensure that we've only received one command to run.
136141
fn is_some<T>(x : &Option<T>) -> bool {
@@ -141,6 +146,8 @@ pub fn parse() -> Result<Command> {
141146
show_stats,
142147
start_server,
143148
stop_server,
149+
zero_stats,
150+
package_toolchain,
144151
is_some(&cmd),
145152
].iter()
146153
.filter(|&&x| x).count() > 1 {
@@ -158,6 +165,11 @@ pub fn parse() -> Result<Command> {
158165
Ok(Command::StopServer)
159166
} else if zero_stats {
160167
Ok(Command::ZeroStats)
168+
} else if package_toolchain {
169+
let mut values = matches.values_of_os("package-toolchain").unwrap();
170+
assert!(values.len() == 2);
171+
let (executable, out) = (values.next().unwrap(), values.next().unwrap());
172+
Ok(Command::PackageToolchain(executable.into(), out.into()))
161173
} else if let Some(mut args) = cmd {
162174
if let Some(exe) = args.next() {
163175
let cmdline = args.map(|s| s.to_owned()).collect::<Vec<_>>();

src/commands.rs

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -618,6 +618,36 @@ pub fn run_command(cmd: Command) -> Result<i32> {
618618
let stats = request_shutdown(server)?;
619619
stats.print();
620620
}
621+
Command::ZeroStats => {
622+
trace!("Command::ZeroStats");
623+
let conn = connect_or_start_server(get_port())?;
624+
let stats = request_zero_stats(conn).chain_err(|| {
625+
"couldn't zero stats on server"
626+
})?;
627+
stats.print();
628+
}
629+
#[cfg(feature = "dist-client")]
630+
Command::PackageToolchain(executable, out) => {
631+
use compiler;
632+
use futures_cpupool::CpuPool;
633+
634+
trace!("Command::PackageToolchain({})", executable.display());
635+
let mut core = Core::new()?;
636+
let jobserver = unsafe { Client::new() };
637+
let creator = ProcessCommandCreator::new(&core.handle(), &jobserver);
638+
let env: Vec<_> = env::vars_os().collect();
639+
let pool = CpuPool::new(1);
640+
let out_file = File::create(out)?;
641+
642+
let compiler = compiler::get_compiler_info(&creator, &executable, &env, &pool);
643+
let packager = compiler.map(|c| c.get_toolchain_packager());
644+
let res = packager.and_then(|p| p.write_pkg(out_file));
645+
core.run(res)?
646+
}
647+
#[cfg(not(feature = "dist-client"))]
648+
Command::PackageToolchain(_executable, _out) => {
649+
bail!("Toolchain packaging not compiled in")
650+
}
621651
Command::Compile { exe, cmdline, cwd, env_vars } => {
622652
trace!("Command::Compile {{ {:?}, {:?}, {:?} }}", exe, cmdline, cwd);
623653
let jobserver = unsafe { Client::new() };
@@ -637,14 +667,6 @@ pub fn run_command(cmd: Command) -> Result<i32> {
637667
"failed to execute compile"
638668
})
639669
}
640-
Command::ZeroStats => {
641-
trace!("Command::ZeroStats");
642-
let conn = connect_or_start_server(get_port())?;
643-
let stats = request_zero_stats(conn).chain_err(|| {
644-
"couldn't zero stats on server"
645-
})?;
646-
stats.print();
647-
}
648670
}
649671

650672
Ok(0)

src/compiler/c.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,10 @@ impl <I> CCompiler<I>
187187

188188
impl<T: CommandCreatorSync, I: CCompilerImpl> Compiler<T> for CCompiler<I> {
189189
fn kind(&self) -> CompilerKind { CompilerKind::C(self.compiler.kind()) }
190+
#[cfg(feature = "dist-client")]
191+
fn get_toolchain_packager(&self) -> Box<pkg::ToolchainPackager> {
192+
Box::new(CToolchainPackager { executable: self.executable.clone() })
193+
}
190194
fn parse_arguments(&self,
191195
arguments: &[OsString],
192196
cwd: &Path) -> CompilerArguments<Box<CompilerHasher<T> + 'static>> {
@@ -362,7 +366,7 @@ impl pkg::ToolchainPackager for CToolchainPackager {
362366
use std::env;
363367
use std::os::unix::ffi::OsStrExt;
364368

365-
info!("Packaging C compiler");
369+
info!("Packaging C compiler for executable {}", self.executable.display());
366370
// TODO: write our own, since this is GPL
367371
let curdir = env::current_dir().unwrap();
368372
env::set_current_dir("/tmp").unwrap();

src/compiler/compiler.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,9 @@ pub trait Compiler<T>: Send + 'static
9393
{
9494
/// Return the kind of compiler.
9595
fn kind(&self) -> CompilerKind;
96+
/// Retrieve a packager
97+
#[cfg(feature = "dist-client")]
98+
fn get_toolchain_packager(&self) -> Box<pkg::ToolchainPackager>;
9699
/// Determine whether `arguments` are supported by this compiler.
97100
fn parse_arguments(&self,
98101
arguments: &[OsString],

src/compiler/rust.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -388,6 +388,10 @@ impl<T> Compiler<T> for Rust
388388
where T: CommandCreatorSync,
389389
{
390390
fn kind(&self) -> CompilerKind { CompilerKind::Rust }
391+
#[cfg(feature = "dist-client")]
392+
fn get_toolchain_packager(&self) -> Box<pkg::ToolchainPackager> {
393+
Box::new(RustToolchainPackager { sysroot: self.sysroot.clone() })
394+
}
391395
/// Parse `arguments` as rustc command-line arguments, determine if
392396
/// we can cache the result of compilation. This is only intended to
393397
/// cover a subset of rustc invocations, primarily focused on those

0 commit comments

Comments
 (0)