Skip to content

Commit 25684b7

Browse files
aidanhsluser
authored andcommitted
Cheat when creating rlibs by only sending metadata
1 parent 147f067 commit 25684b7

File tree

5 files changed

+54
-7
lines changed

5 files changed

+54
-7
lines changed

Cargo.lock

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

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ name = "sccache-dist"
2121
required-features = ["dist-server"]
2222

2323
[dependencies]
24+
ar = "0.6"
2425
atty = "0.2.6"
2526
base64 = "0.9.0"
2627
bincode = "1"

src/compiler/compiler.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -380,7 +380,7 @@ fn dist_or_local_compile<T>(dist_client: Arc<dist::Client>,
380380
dist::SubmitToolchainResult::JobNotFound |
381381
dist::SubmitToolchainResult::CannotCache => panic!(),
382382
}
383-
}).map_err(Into::into))
383+
}).chain_err(|| "Could not submit toolchain"))
384384
},
385385
dist::AllocJobResult::Success { job_alloc, need_toolchain: false } =>
386386
f_ok(job_alloc),

src/compiler/rust.rs

Lines changed: 41 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15+
use ar;
1516
use compiler::{Cacheable, ColorMode, Compiler, CompilerArguments, CompileCommand, CompilerHasher, CompilerKind,
1617
Compilation, HashResult};
1718
use compiler::args::*;
@@ -89,6 +90,8 @@ pub struct ParsedArguments {
8990
staticlibs: Vec<PathBuf>,
9091
/// The crate name passed to --crate-name.
9192
crate_name: String,
93+
/// The crate types that will be generated
94+
crate_types: CrateTypes,
9295
/// If dependency info is being emitted, the name of the dep info file.
9396
dep_info: Option<PathBuf>,
9497
/// The value of any `--color` option passed on the commandline.
@@ -112,14 +115,16 @@ pub struct RustCompilation {
112115
crate_link_paths: Vec<PathBuf>,
113116
/// The crate name being compiled.
114117
crate_name: String,
118+
/// The crate types that will be generated
119+
crate_types: CrateTypes,
115120
/// The current working directory
116121
cwd: PathBuf,
117122
/// The environment variables
118123
env_vars: Vec<(OsString, OsString)>,
119124
}
120125

121126
// The selection of crate types for this compilation
122-
#[derive(Debug, Clone)]
127+
#[derive(Debug, Clone, PartialEq)]
123128
pub struct CrateTypes {
124129
rlib: bool,
125130
staticlib: bool,
@@ -791,6 +796,7 @@ fn parse_arguments(arguments: &[OsString], cwd: &Path) -> CompilerArguments<Pars
791796
CompilerArguments::Ok(ParsedArguments {
792797
arguments: args,
793798
output_dir: output_dir.into(),
799+
crate_types,
794800
externs: externs,
795801
crate_link_paths,
796802
staticlibs: staticlibs,
@@ -812,7 +818,7 @@ impl<T> CompilerHasher<T> for RustHasher
812818
-> SFuture<HashResult>
813819
{
814820
let me = *self;
815-
let RustHasher { executable, sysroot, compiler_shlibs_digests, parsed_args: ParsedArguments { arguments, output_dir, externs, crate_link_paths, staticlibs, crate_name, dep_info, color_mode: _ } } = me;
821+
let RustHasher { executable, sysroot, compiler_shlibs_digests, parsed_args: ParsedArguments { arguments, output_dir, externs, crate_link_paths, staticlibs, crate_name, crate_types, dep_info, color_mode: _ } } = me;
816822
trace!("[{}]: generate_hash_key", crate_name);
817823
// TODO: this doesn't produce correct arguments if they should be concatenated - should use iter_os_strings
818824
let os_string_arguments: Vec<(OsString, Option<OsString>)> = arguments.iter()
@@ -938,7 +944,8 @@ impl<T> CompilerHasher<T> for RustHasher
938944
inputs: inputs,
939945
outputs: outputs,
940946
crate_link_paths,
941-
crate_name: crate_name,
947+
crate_name,
948+
crate_types,
942949
cwd,
943950
env_vars,
944951
}),
@@ -1005,7 +1012,7 @@ impl Compilation for RustCompilation {
10051012

10061013
#[cfg(feature = "dist-client")]
10071014
fn into_dist_inputs_creator(self: Box<Self>, path_transformer: &mut dist::PathTransformer) -> Result<(Box<FnMut(&mut io::Write)>, Box<pkg::CompilerPackager>)> {
1008-
let RustCompilation { inputs, crate_link_paths, sysroot, .. } = *{self};
1015+
let RustCompilation { inputs, crate_link_paths, sysroot, crate_types, .. } = *{self};
10091016
trace!("Dist inputs: inputs={:?} crate_link_paths={:?}", inputs, crate_link_paths);
10101017

10111018
let mut tar_inputs = vec![];
@@ -1046,10 +1053,37 @@ impl Compilation for RustCompilation {
10461053
// There are almost certainly duplicates from explicit externs also within the lib search paths
10471054
all_tar_inputs.dedup();
10481055

1056+
// If we're just creating an rlib then the only thing inspected inside dependency rlibs is the
1057+
// metadata, in which case we can create a trimmed rlib (which is actually a .a) with the metadata
1058+
let can_trim_rlibs = if let CrateTypes { rlib: true, staticlib: false } = crate_types { true } else { false };
1059+
10491060
for (input_path, dist_input_path) in all_tar_inputs.iter() {
10501061
let mut file_header = pkg::make_tar_header(input_path, dist_input_path).unwrap();
1051-
file_header.set_cksum();
1052-
builder.append(&file_header, fs::File::open(input_path).unwrap()).unwrap();
1062+
let file = fs::File::open(input_path).unwrap();
1063+
if can_trim_rlibs && input_path.extension().unwrap() == "rlib" {
1064+
let mut archive = ar::Archive::new(file);
1065+
1066+
while let Some(entry_result) = archive.next_entry() {
1067+
let mut entry = entry_result.unwrap();
1068+
if entry.header().identifier() != b"rust.metadata.bin" {
1069+
continue
1070+
}
1071+
let mut metadata = vec![];
1072+
io::copy(&mut entry, &mut metadata).unwrap();
1073+
let mut metadata_ar = vec![];
1074+
{
1075+
let mut ar_builder = ar::Builder::new(&mut metadata_ar);
1076+
ar_builder.append(entry.header(), metadata.as_slice()).unwrap()
1077+
}
1078+
file_header.set_size(metadata_ar.len() as u64);
1079+
file_header.set_cksum();
1080+
builder.append(&file_header, metadata_ar.as_slice()).unwrap();
1081+
break
1082+
}
1083+
} else {
1084+
file_header.set_cksum();
1085+
builder.append(&file_header, file).unwrap()
1086+
}
10531087
}
10541088

10551089
// Finish archive
@@ -1416,6 +1450,7 @@ c:/foo/bar.rs:
14161450
crate_link_paths: vec![],
14171451
staticlibs: vec![f.tempdir.path().join("libbaz.a")],
14181452
crate_name: "foo".into(),
1453+
crate_types: CrateTypes { rlib: true, staticlib: false },
14191454
dep_info: None,
14201455
color_mode: ColorMode::Auto,
14211456
}

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
#![recursion_limit = "128"]
1616

17+
extern crate ar;
1718
extern crate atty;
1819
extern crate base64;
1920
extern crate bincode;

0 commit comments

Comments
 (0)