Skip to content

Commit d45f877

Browse files

File tree

4 files changed

+47
-34
lines changed

4 files changed

+47
-34
lines changed
 

‎src/Cargo.lock

+1
Original file line numberDiff line numberDiff line change
@@ -2373,6 +2373,7 @@ dependencies = [
23732373
"rls-span 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
23742374
"rustc 0.0.0",
23752375
"rustc-serialize 0.3.24 (registry+https://github.com/rust-lang/crates.io-index)",
2376+
"rustc_codegen_utils 0.0.0",
23762377
"rustc_data_structures 0.0.0",
23772378
"rustc_target 0.0.0",
23782379
"rustc_typeck 0.0.0",

‎src/librustc_save_analysis/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ crate-type = ["dylib"]
1212
log = "0.4"
1313
rustc = { path = "../librustc" }
1414
rustc_data_structures = { path = "../librustc_data_structures" }
15+
rustc_codegen_utils = { path = "../librustc_codegen_utils" }
1516
rustc_target = { path = "../librustc_target" }
1617
rustc_typeck = { path = "../librustc_typeck" }
1718
syntax = { path = "../libsyntax" }

‎src/librustc_save_analysis/dump_visitor.rs

+19-22
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,9 @@ use rustc::hir::def_id::DefId;
2828
use rustc::ty::{self, TyCtxt};
2929
use rustc_data_structures::fx::FxHashSet;
3030

31-
use std::path::{Path, PathBuf};
31+
use std::path::Path;
3232
use std::env;
33+
use std::fs;
3334

3435
use syntax::ast::{self, Attribute, NodeId, PatKind, CRATE_NODE_ID};
3536
use syntax::parse::token;
@@ -172,11 +173,20 @@ impl<'l, 'tcx: 'l, 'll, O: DumpOutput + 'll> DumpVisitor<'l, 'tcx, 'll, O> {
172173
self.dumper.crate_prelude(data);
173174
}
174175

175-
pub fn dump_compilation_options(&mut self) {
176-
// Apply possible `remap-path-prefix` remapping to the raw invocation
177-
let invocation = {
176+
pub fn dump_compilation_options(&mut self, crate_name: &str) {
177+
// Apply possible `remap-path-prefix` remapping to the raw command
178+
let command = {
179+
let mapping = self.tcx.sess.source_map().path_mapping();
180+
let remap_arg = |x: &str| -> String {
181+
match fs::canonicalize(x) {
182+
Ok(path) => mapping.map_prefix(path).0.to_str().unwrap().to_owned(),
183+
Err(_) => x.to_owned(), // Probably not a path, ignore
184+
}
185+
};
186+
178187
let remap_arg_indices = {
179188
let mut indices = FxHashSet();
189+
// rustc args are guaranteed to be valid UTF-8 (checked early)
180190
for (i, e) in env::args().enumerate() {
181191
if e.starts_with("--remap-path-prefix=") {
182192
indices.insert(i);
@@ -188,19 +198,10 @@ impl<'l, 'tcx: 'l, 'll, O: DumpOutput + 'll> DumpVisitor<'l, 'tcx, 'll, O> {
188198
indices
189199
};
190200

191-
let args_without_remap_args = env::args()
201+
let args = env::args()
192202
.enumerate()
193203
.filter(|(i, _)| !remap_arg_indices.contains(i))
194-
.map(|(_, e)| e);
195-
196-
let mapping = self.tcx.sess.source_map().path_mapping();
197-
let remap_arg = |x: &str| -> String {
198-
mapping.map_prefix(PathBuf::from(x)).0.to_str().unwrap().to_owned()
199-
};
200-
201-
// Naively attempt to remap every argument
202-
let args = args_without_remap_args
203-
.map(|elem| {
204+
.map(|(_, elem)| {
204205
let mut arg = elem.splitn(2, '=');
205206
match (arg.next(), arg.next()) {
206207
// Apart from `--remap...`, in `a=b` args usually only
@@ -214,14 +215,10 @@ impl<'l, 'tcx: 'l, 'll, O: DumpOutput + 'll> DumpVisitor<'l, 'tcx, 'll, O> {
214215
args.as_slice().join(" ")
215216
};
216217

217-
let opts = &self.tcx.sess.opts;
218-
219218
let data = CompilationOptions {
220-
invocation,
221-
crate_name: opts.crate_name.clone(),
222-
test: opts.test,
223-
sysroot: opts.maybe_sysroot.clone(),
224-
target_triple: opts.target_triple.to_string(),
219+
directory: self.tcx.sess.working_dir.0.clone(),
220+
command,
221+
output: self.save_ctxt.compilation_output(crate_name),
225222
};
226223

227224
self.dumper.compilation_opts(data);

‎src/librustc_save_analysis/lib.rs

+26-12
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ extern crate rustc;
2323
#[macro_use]
2424
extern crate log;
2525
extern crate rustc_data_structures;
26+
extern crate rustc_codegen_utils;
2627
extern crate rustc_serialize;
2728
extern crate rustc_target;
2829
extern crate rustc_typeck;
@@ -45,9 +46,10 @@ use rustc::hir::def::Def as HirDef;
4546
use rustc::hir::Node;
4647
use rustc::hir::def_id::{DefId, LOCAL_CRATE};
4748
use rustc::middle::cstore::ExternCrate;
48-
use rustc::session::config::CrateType;
49+
use rustc::session::config::{CrateType, OutputType};
4950
use rustc::ty::{self, TyCtxt};
5051
use rustc_typeck::hir_ty_to_ty;
52+
use rustc_codegen_utils::link::{filename_for_metadata, out_filename};
5153

5254
use std::cell::Cell;
5355
use std::default::Default;
@@ -111,6 +113,24 @@ impl<'l, 'tcx: 'l> SaveContext<'l, 'tcx> {
111113
}
112114
}
113115

116+
// Returns path to the compilation output (e.g. libfoo-12345678.rmeta)
117+
pub fn compilation_output(&self, crate_name: &str) -> PathBuf {
118+
let sess = &self.tcx.sess;
119+
// Save-analysis is emitted per whole session, not per each crate type
120+
let crate_type = sess.crate_types.borrow()[0];
121+
let outputs = &*self.tcx.output_filenames(LOCAL_CRATE);
122+
123+
if outputs.outputs.contains_key(&OutputType::Metadata) {
124+
filename_for_metadata(sess, crate_name, outputs)
125+
} else if outputs.outputs.should_codegen() {
126+
out_filename(sess, crate_type, outputs, crate_name)
127+
} else {
128+
// Otherwise it's only a DepInfo, in which case we return early and
129+
// not even reach the analysis stage.
130+
unreachable!()
131+
}
132+
}
133+
114134
// List external crates used by the current crate.
115135
pub fn get_external_crates(&self) -> Vec<ExternalCrateData> {
116136
let mut result = Vec::with_capacity(self.tcx.crates().len());
@@ -139,15 +159,9 @@ impl<'l, 'tcx: 'l> SaveContext<'l, 'tcx> {
139159
disambiguator: self.tcx.crate_disambiguator(n).to_fingerprint().as_value(),
140160
},
141161
source: CrateSource {
142-
dylib: src.dylib.as_ref().map(|(ref path, _)|
143-
map_prefix(path).display().to_string()
144-
),
145-
rlib: src.rlib.as_ref().map(|(ref path, _)|
146-
map_prefix(path).display().to_string()
147-
),
148-
rmeta: src.rmeta.as_ref().map(|(ref path, _)|
149-
map_prefix(path).display().to_string()
150-
),
162+
dylib: src.dylib.as_ref().map(|(path, _)| map_prefix(path)),
163+
rlib: src.rlib.as_ref().map(|(path, _)| map_prefix(path)),
164+
rmeta: src.rmeta.as_ref().map(|(path, _)| map_prefix(path)),
151165
}
152166
});
153167
}
@@ -1103,7 +1117,7 @@ impl<'a> SaveHandler for DumpHandler<'a> {
11031117
let mut visitor = DumpVisitor::new(save_ctxt, &mut dumper);
11041118

11051119
visitor.dump_crate_info(cratename, krate);
1106-
visitor.dump_compilation_options();
1120+
visitor.dump_compilation_options(cratename);
11071121
visit::walk_crate(&mut visitor, krate);
11081122
}
11091123
}
@@ -1129,7 +1143,7 @@ impl<'b> SaveHandler for CallbackHandler<'b> {
11291143
let mut visitor = DumpVisitor::new(save_ctxt, &mut dumper);
11301144

11311145
visitor.dump_crate_info(cratename, krate);
1132-
visitor.dump_compilation_options();
1146+
visitor.dump_compilation_options(cratename);
11331147
visit::walk_crate(&mut visitor, krate);
11341148
}
11351149
}

0 commit comments

Comments
 (0)
Please sign in to comment.