Skip to content

Commit 5524939

Browse files
aidanhsluser
authored andcommitted
Make an InputsPackaging trait, conditional compilation, rearrangement
1 parent 79850e7 commit 5524939

File tree

12 files changed

+433
-382
lines changed

12 files changed

+433
-382
lines changed

Cargo.lock

Lines changed: 0 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,10 @@ name = "sccache-dist"
2121
required-features = ["dist-server"]
2222

2323
[dependencies]
24-
ar = "0.6"
24+
ar = { version = "0.6", optional = true }
2525
atty = "0.2.6"
2626
base64 = "0.9.0"
2727
bincode = "1"
28-
boxfnonce = "0.1"
2928
byteorder = "1.0"
3029
bytes = "0.4"
3130
chrono = { version = "0.4", optional = true }
@@ -114,7 +113,7 @@ memcached = ["memcached-rs"]
114113
# Enable features that require unstable features of Nightly Rust.
115114
unstable = []
116115
# Enables distributed support in the sccache client
117-
dist-client = ["flate2", "reqwest"]
116+
dist-client = ["ar", "flate2", "reqwest"]
118117
# Enables the sccache-dist binary
119118
dist-server = ["arraydeque", "crossbeam-utils", "jsonwebtoken", "flate2", "libmount", "nix", "reqwest", "rouille"]
120119

src/compiler/c.rs

Lines changed: 46 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -14,21 +14,24 @@
1414

1515
use compiler::{Cacheable, ColorMode, Compiler, CompilerArguments, CompileCommand, CompilerHasher, CompilerKind,
1616
Compilation, HashResult};
17-
use compiler::pkg;
1817
use dist;
18+
#[cfg(feature = "dist-client")]
19+
use dist::pkg;
1920
use futures::Future;
2021
use futures_cpupool::CpuPool;
2122
use mock_command::CommandCreatorSync;
2223
use std::borrow::Cow;
2324
use std::collections::{HashMap, HashSet};
2425
use std::ffi::{OsStr, OsString};
2526
use std::fmt;
26-
use std::fs::File;
27+
#[cfg(feature = "dist-client")]
28+
#[cfg(all(target_os = "linux", target_arch = "x86_64"))]
29+
use std::fs;
2730
use std::hash::Hash;
31+
#[cfg(feature = "dist-client")]
2832
use std::io;
2933
use std::path::{Path, PathBuf};
3034
use std::process;
31-
use tar;
3235
use util::{HashToDigest, Digest};
3336

3437
use errors::*;
@@ -120,6 +123,7 @@ impl Language {
120123
/// A generic implementation of the `Compilation` trait for C/C++ compilers.
121124
struct CCompilation<I: CCompilerImpl> {
122125
parsed_args: ParsedArguments,
126+
#[cfg(feature = "dist-client")]
123127
preprocessed_input: Vec<u8>,
124128
executable: PathBuf,
125129
compiler: I,
@@ -262,6 +266,7 @@ impl<T, I> CompilerHasher<T> for CCompilerHasher<I>
262266
key: key,
263267
compilation: Box::new(CCompilation {
264268
parsed_args: parsed_args,
269+
#[cfg(feature = "dist-client")]
265270
preprocessed_input: preprocessor_result.stdout,
266271
executable: executable,
267272
compiler: compiler,
@@ -293,34 +298,22 @@ impl<I: CCompilerImpl> Compilation for CCompilation<I> {
293298
fn generate_compile_commands(&self, path_transformer: &mut dist::PathTransformer)
294299
-> Result<(CompileCommand, Option<dist::CompileCommand>, Cacheable)>
295300
{
296-
let CCompilation { ref parsed_args, ref executable, ref compiler, preprocessed_input: _, ref cwd, ref env_vars } = *self;
301+
let CCompilation { ref parsed_args, ref executable, ref compiler, ref cwd, ref env_vars, .. } = *self;
297302
compiler.generate_compile_commands(path_transformer, executable, parsed_args, cwd, env_vars)
298303
}
299304

300305
#[cfg(feature = "dist-client")]
301-
fn into_dist_inputs_creator(self: Box<Self>, path_transformer: &mut dist::PathTransformer) -> Result<(Box<FnMut(&mut io::Write) + Send>, Box<pkg::CompilerPackager>)> {
306+
fn into_dist_packagers(self: Box<Self>, path_transformer: &mut dist::PathTransformer) -> Result<(Box<pkg::InputsPackager>, Box<pkg::ToolchainPackager>)> {
302307
let CCompilation { parsed_args, cwd, preprocessed_input, executable, .. } = *{self};
303308
trace!("Dist inputs: {:?}", parsed_args.input);
304309

305310
let input_path = cwd.join(&parsed_args.input);
306311
let input_path = pkg::simplify_path(&input_path)?;
307312
let dist_input_path = path_transformer.to_dist(&input_path).unwrap();
308-
let preprocessed_input = preprocessed_input;
309-
310-
let toolchain_creator = Box::new(CCompilerPackager { executable });
311-
let inputs_creator = Box::new(move |wtr: &mut io::Write| {
312-
let mut builder = tar::Builder::new(wtr);
313313

314-
let mut file_header = pkg::make_tar_header(&input_path, &dist_input_path).unwrap();
315-
file_header.set_size(preprocessed_input.len() as u64); // The metadata is from non-preprocessed
316-
file_header.set_cksum();
317-
builder.append(&file_header, preprocessed_input.as_slice()).unwrap();
318-
319-
// Finish archive
320-
let _ = builder.into_inner().unwrap();
321-
});
322-
323-
Ok((inputs_creator, toolchain_creator))
314+
let inputs_packager = Box::new(CInputsPackager { input_path, dist_input_path, preprocessed_input });
315+
let toolchain_packager = Box::new(CToolchainPackager { executable });
316+
Ok((inputs_packager, toolchain_packager))
324317
}
325318

326319
fn outputs<'a>(&'a self) -> Box<Iterator<Item=(&'a str, &'a Path)> + 'a>
@@ -329,17 +322,44 @@ impl<I: CCompilerImpl> Compilation for CCompilation<I> {
329322
}
330323
}
331324

332-
struct CCompilerPackager {
333-
#[allow(dead_code)]
325+
#[cfg(feature = "dist-client")]
326+
struct CInputsPackager {
327+
input_path: PathBuf,
328+
dist_input_path: String,
329+
preprocessed_input: Vec<u8>,
330+
}
331+
332+
#[cfg(feature = "dist-client")]
333+
impl pkg::InputsPackager for CInputsPackager {
334+
fn write_inputs(self: Box<Self>, wtr: &mut io::Write) -> Result<()> {
335+
use tar;
336+
337+
let CInputsPackager { input_path, dist_input_path, preprocessed_input } = *{self};
338+
339+
let mut builder = tar::Builder::new(wtr);
340+
341+
let mut file_header = pkg::make_tar_header(&input_path, &dist_input_path)?;
342+
file_header.set_size(preprocessed_input.len() as u64); // The metadata is from non-preprocessed
343+
file_header.set_cksum();
344+
builder.append(&file_header, preprocessed_input.as_slice())?;
345+
346+
// Finish archive
347+
let _ = builder.into_inner();
348+
Ok(())
349+
}
350+
}
351+
352+
#[cfg(feature = "dist-client")]
353+
#[allow(unused)]
354+
struct CToolchainPackager {
334355
executable: PathBuf,
335356
}
336357

337358
#[cfg(feature = "dist-client")]
338359
#[cfg(all(target_os = "linux", target_arch = "x86_64"))]
339-
impl pkg::CompilerPackager for CCompilerPackager {
340-
fn write_pkg(self: Box<Self>, f: File) -> Result<()> {
360+
impl pkg::ToolchainPackager for CToolchainPackager {
361+
fn write_pkg(self: Box<Self>, f: fs::File) -> Result<()> {
341362
use std::env;
342-
use std::fs;
343363
use std::os::unix::ffi::OsStrExt;
344364

345365
info!("Packaging C compiler");
@@ -354,7 +374,7 @@ impl pkg::CompilerPackager for CCompilerPackager {
354374
let file_line = output.stdout.split(|&b| b == b'\n').find(|line| line.starts_with(b"creating ")).unwrap();
355375
let filename = &file_line[b"creating ".len()..];
356376
let filename = OsStr::from_bytes(filename);
357-
io::copy(&mut File::open(filename).unwrap(), &mut {f}).unwrap();
377+
io::copy(&mut fs::File::open(filename).unwrap(), &mut {f}).unwrap();
358378
fs::remove_file(filename).unwrap();
359379
env::set_current_dir(curdir).unwrap();
360380
Ok(())

src/compiler/compiler.rs

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,10 @@ use compiler::c::{CCompiler, CCompilerKind};
2222
use compiler::clang::Clang;
2323
use compiler::gcc::GCC;
2424
use compiler::msvc::MSVC;
25-
use compiler::pkg::CompilerPackager;
2625
use compiler::rust::Rust;
2726
use dist;
27+
#[cfg(feature = "dist-client")]
28+
use dist::pkg;
2829
use futures::{Future, IntoFuture};
2930
use futures_cpupool::CpuPool;
3031
use mock_command::{
@@ -336,7 +337,6 @@ fn dist_or_local_compile<T>(dist_client: Arc<dist::Client>,
336337
out_pretty: String)
337338
-> SFuture<(Cacheable, process::Output)>
338339
where T: CommandCreatorSync {
339-
use boxfnonce::BoxFnOnce;
340340
use futures::future;
341341
use std::io;
342342

@@ -355,15 +355,14 @@ fn dist_or_local_compile<T>(dist_client: Arc<dist::Client>,
355355
.map(|(_key, path)| path_transformer.to_dist_assert_abs(&cwd.join(path)))
356356
.collect::<Option<_>>()
357357
.unwrap();
358-
compilation.into_dist_inputs_creator(&mut path_transformer)
359-
.map(|(inputs_creator, toolchain_creator)| (path_transformer, dist_compile_cmd, inputs_creator, toolchain_creator, dist_output_paths))
358+
compilation.into_dist_packagers(&mut path_transformer)
359+
.map(|packagers| (path_transformer, dist_compile_cmd, packagers, dist_output_paths))
360360
})
361-
.and_then(move |(path_transformer, mut dist_compile_cmd, inputs_creator, toolchain_creator, dist_output_paths)| {
361+
.and_then(move |(path_transformer, mut dist_compile_cmd, (inputs_packager, toolchain_packager), dist_output_paths)| {
362362
debug!("[{}]: Identifying dist toolchain for {:?}", compile_out_pretty2, local_executable);
363-
let toolchain_creator_cb = BoxFnOnce::from(move |f| toolchain_creator.write_pkg(f));
364363
// TODO: put on a thread
365364
let (dist_toolchain, maybe_dist_compile_executable) =
366-
ftry!(dist_client.put_toolchain(&local_executable, &weak_toolchain_key, toolchain_creator_cb));
365+
ftry!(dist_client.put_toolchain(&local_executable, &weak_toolchain_key, toolchain_packager));
367366
if let Some(dist_compile_executable) = maybe_dist_compile_executable {
368367
dist_compile_cmd.executable = dist_compile_executable;
369368
}
@@ -391,7 +390,7 @@ fn dist_or_local_compile<T>(dist_client: Arc<dist::Client>,
391390
alloc
392391
.and_then(move |job_alloc| {
393392
debug!("[{}]: Running job", compile_out_pretty2);
394-
dist_client.do_run_job(job_alloc, dist_compile_cmd, dist_output_paths, inputs_creator)
393+
dist_client.do_run_job(job_alloc, dist_compile_cmd, dist_output_paths, inputs_packager)
395394
.map_err(Into::into)
396395
})
397396
})
@@ -435,8 +434,8 @@ pub trait Compilation {
435434
/// Create a function that will create the inputs used to perform a distributed compilation
436435
// TODO: It's more correct to have a FnBox or Box<FnOnce> here
437436
#[cfg(feature = "dist-client")]
438-
fn into_dist_inputs_creator(self: Box<Self>, _path_transformer: &mut dist::PathTransformer)
439-
-> Result<(Box<FnMut(&mut Write) + Send>, Box<CompilerPackager>)> {
437+
fn into_dist_packagers(self: Box<Self>, _path_transformer: &mut dist::PathTransformer)
438+
-> Result<(Box<pkg::InputsPackager>, Box<pkg::ToolchainPackager>)> {
440439

441440
bail!("distributed compilation not implemented")
442441
}

src/compiler/mod.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ mod clang;
2020
mod compiler;
2121
mod gcc;
2222
mod msvc;
23-
mod pkg;
2423
mod rust;
2524

2625
pub use compiler::compiler::*;

0 commit comments

Comments
 (0)