Skip to content

Commit 856641c

Browse files
committed
Be a bit more careful not to crash if the compiler doesn't support --crate-type metadata
1 parent e469925 commit 856641c

File tree

3 files changed

+24
-14
lines changed

3 files changed

+24
-14
lines changed

src/cargo/ops/cargo_compile.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ pub struct CompileOptions<'a> {
6565
pub target_rustc_args: Option<&'a [String]>,
6666
}
6767

68-
#[derive(Clone, Copy, PartialEq)]
68+
#[derive(Clone, Copy, PartialEq, Debug)]
6969
pub enum CompileMode {
7070
Test,
7171
Build,

src/cargo/ops/cargo_rustc/context.rs

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -174,13 +174,17 @@ impl<'a, 'cfg> Context<'a, 'cfg> {
174174
"RUSTFLAGS")?;
175175
let mut process = self.config.rustc()?.process();
176176
process.arg("-")
177-
.arg("--crate-name").arg("_")
177+
.arg("--crate-name").arg("___")
178178
.arg("--print=file-names")
179179
.args(&rustflags)
180180
.env_remove("RUST_LOG");
181181

182182
for crate_type in crate_types {
183-
process.arg("--crate-type").arg(crate_type);
183+
// Here and below we'll skip the metadata crate-type because it is
184+
// not supported by older compilers. We'll do this one manually.
185+
if crate_type != "metadata" {
186+
process.arg("--crate-type").arg(crate_type);
187+
}
184188
}
185189
if kind == Kind::Target {
186190
process.arg("--target").arg(&self.target_triple());
@@ -204,31 +208,37 @@ impl<'a, 'cfg> Context<'a, 'cfg> {
204208
let mut map = HashMap::new();
205209
for crate_type in crate_types {
206210
let not_supported = error.lines().any(|line| {
207-
line.contains("unsupported crate type") &&
208-
line.contains(crate_type)
211+
(line.contains("unsupported crate type") ||
212+
line.contains("unknown crate type")) &&
213+
line.contains(crate_type)
209214
});
210215
if not_supported {
211-
if crate_type == "metadata" {
212-
bail!("compiler does not support `--crate-type metadata`, \
213-
cannot run `cargo check`.");
214-
}
215216
map.insert(crate_type.to_string(), None);
216-
continue
217+
continue;
218+
}
219+
if crate_type == "metadata" {
220+
continue;
217221
}
218222
let line = match lines.next() {
219223
Some(line) => line,
220224
None => bail!("malformed output when learning about \
221225
target-specific information from rustc"),
222226
};
223-
let mut parts = line.trim().split('_');
227+
let mut parts = line.trim().split("___");
224228
let prefix = parts.next().unwrap();
225229
let suffix = match parts.next() {
226230
Some(part) => part,
227231
None => bail!("output of --print=file-names has changed in \
228232
the compiler, cannot parse"),
229233
};
230-
map.insert(crate_type.to_string(),
231-
Some((prefix.to_string(), suffix.to_string())));
234+
235+
map.insert(crate_type.to_string(), Some((prefix.to_string(), suffix.to_string())));
236+
}
237+
238+
// Manually handle the metadata case. If it is not supported by the
239+
// compiler we'll error out elsewhere.
240+
if crate_types.contains("metadata") {
241+
map.insert("metadata".to_string(), Some(("lib".to_owned(), ".rmeta".to_owned())));
232242
}
233243

234244
let cfg = if has_cfg {

src/cargo/util/toml.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1249,7 +1249,7 @@ fn build_profiles(profiles: &Option<TomlProfiles>) -> Profiles {
12491249
profiles.and_then(|p| p.doc.as_ref())),
12501250
custom_build: Profile::default_custom_build(),
12511251
check: merge(Profile::default_check(),
1252-
profiles.and_then(|p| p.dev.as_ref())),
1252+
profiles.and_then(|p| p.dev.as_ref())),
12531253
};
12541254
// The test/bench targets cannot have panic=abort because they'll all get
12551255
// compiled with --test which requires the unwind runtime currently

0 commit comments

Comments
 (0)