Skip to content

Commit 9c4894d

Browse files
committed
Move ELF note parsing into cargo-subcommand-metadata optional feature
1 parent 37ad8f4 commit 9c4894d

File tree

6 files changed

+24
-22
lines changed

6 files changed

+24
-22
lines changed

Cargo.toml

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ path = "src/cargo/lib.rs"
1919
atty = "0.2"
2020
bytesize = "1.0"
2121
cargo-platform = { path = "crates/cargo-platform", version = "0.1.2" }
22+
cargo-subcommand-metadata = { path = "crates/cargo-subcommand-metadata", version = "0.1.0", features = ["parse"] }
2223
cargo-util = { path = "crates/cargo-util", version = "0.2.1" }
2324
crates-io = { path = "crates/crates-io", version = "0.34.0" }
2425
curl = { version = "0.4.43", features = ["http2"] }
@@ -72,11 +73,6 @@ itertools = "0.10.0"
7273
# for more information.
7374
rustc-workspace-hack = "1.0.0"
7475

75-
[target.'cfg(target_os = "linux")'.dependencies]
76-
cargo-subcommand-metadata = { path = "crates/cargo-subcommand-metadata", version = "0.1.0" }
77-
memmap = "0.7"
78-
object = "0.28"
79-
8076
[target.'cfg(windows)'.dependencies]
8177
fwdansi = "1.1.0"
8278

crates/cargo-subcommand-metadata/Cargo.toml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,10 @@ edition = "2021"
55
license = "MIT OR Apache-2.0"
66
repository = "https://github.com/rust-lang/cargo"
77
description = "Embed metadata into a Cargo subcommand, so that `cargo --list` can show a description of the subcommand"
8+
9+
[target.'cfg(target_os = "linux")'.dependencies]
10+
memmap = { version = "0.7", optional = true }
11+
object = { version = "0.28", optional = true }
12+
13+
[features]
14+
parse = ["memmap", "object"]

crates/cargo-subcommand-metadata/src/lib.rs

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
#[cfg(feature = "parse")]
2+
pub mod parse;
3+
14
/// Cargo's name for the purpose of ELF notes.
25
///
36
/// The `name` field of an ELF note is designated to hold the entry's "owner" or
@@ -82,16 +85,14 @@ macro_rules! description {
8285

8386
#[used]
8487
#[link_section = ".note.cargo.subcommand"]
85-
static ELF_NOTE: ElfNote = {
86-
ElfNote {
87-
namesz: $crate::ELF_NOTE_NAME.len() as u32 + 1,
88-
descsz: CARGO_SUBCOMMAND_DESCRIPTION.len() as u32,
89-
ty: $crate::ElfNoteType::Description,
90-
name: unsafe { *$crate::ELF_NOTE_NAME.as_ptr().cast() },
91-
name_padding: $crate::private::padding(),
92-
desc: unsafe { *CARGO_SUBCOMMAND_DESCRIPTION.as_ptr().cast() },
93-
desc_padding: $crate::private::padding(),
94-
}
88+
static ELF_NOTE: ElfNote = ElfNote {
89+
namesz: $crate::ELF_NOTE_NAME.len() as u32 + 1,
90+
descsz: CARGO_SUBCOMMAND_DESCRIPTION.len() as u32,
91+
ty: $crate::ElfNoteType::Description,
92+
name: unsafe { *$crate::ELF_NOTE_NAME.as_ptr().cast() },
93+
name_padding: $crate::private::padding(),
94+
desc: unsafe { *CARGO_SUBCOMMAND_DESCRIPTION.as_ptr().cast() },
95+
desc_padding: $crate::private::padding(),
9596
};
9697
};
9798
};

src/bin/cargo/subcommand_metadata.rs renamed to crates/cargo-subcommand-metadata/src/parse.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use std::path::Path;
22

3-
pub(crate) fn description(path: &Path) -> Option<String> {
3+
pub fn description(path: &Path) -> Option<String> {
44
implementation::description(path)
55
}
66

@@ -29,9 +29,8 @@ mod implementation {
2929
if section_header.name(endian, string_table).ok() == Some(b".note.cargo.subcommand") {
3030
if let Ok(Some(mut notes)) = section_header.notes(endian, data) {
3131
while let Ok(Some(note)) = notes.next() {
32-
if note.name() == cargo_subcommand_metadata::ELF_NOTE_NAME.as_bytes()
33-
&& note.n_type(endian)
34-
== cargo_subcommand_metadata::ElfNoteType::Description as u32
32+
if note.name() == crate::ELF_NOTE_NAME.as_bytes()
33+
&& note.n_type(endian) == crate::ElfNoteType::Description as u32
3534
{
3635
if description.is_some() {
3736
return None;

src/bin/cargo/cli.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use anyhow::anyhow;
22
use cargo::core::shell::Shell;
33
use cargo::core::{features, CliUnstable};
44
use cargo::{self, drop_print, drop_println, CliResult, Config};
5+
use cargo_subcommand_metadata as subcommand_metadata;
56
use clap::{AppSettings, Arg, ArgMatches};
67
use itertools::Itertools;
78
use std::collections::HashMap;
@@ -10,7 +11,6 @@ use std::fmt::Write;
1011
use super::commands;
1112
use super::list_commands;
1213
use crate::command_prelude::*;
13-
use crate::subcommand_metadata;
1414
use cargo::core::features::HIDDEN;
1515

1616
lazy_static::lazy_static! {
@@ -123,7 +123,7 @@ Run with 'cargo -Z [FLAG] [SUBCOMMAND]'",
123123
CommandInfo::External { path } => {
124124
if let Some(desc) = known_external_desc {
125125
drop_println!(config, " {:<20} {}", name, desc);
126-
} else if let Some(desc) = subcommand_metadata::description(&path) {
126+
} else if let Some(desc) = subcommand_metadata::parse::description(&path) {
127127
drop_println!(config, " {:<20} {}", name, desc);
128128
} else if is_verbose {
129129
drop_println!(config, " {:<20} {}", name, path.display());

src/bin/cargo/main.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ use std::path::{Path, PathBuf};
1212

1313
mod cli;
1414
mod commands;
15-
mod subcommand_metadata;
1615

1716
use crate::command_prelude::*;
1817

0 commit comments

Comments
 (0)