Skip to content

Commit

Permalink
Move ELF note parsing into cargo-subcommand-metadata optional feature
Browse files Browse the repository at this point in the history
  • Loading branch information
dtolnay committed Oct 2, 2022
1 parent bcff432 commit 8b6c7b7
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 22 deletions.
6 changes: 1 addition & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ path = "src/cargo/lib.rs"
atty = "0.2"
bytesize = "1.0"
cargo-platform = { path = "crates/cargo-platform", version = "0.1.2" }
cargo-subcommand-metadata = { path = "crates/cargo-subcommand-metadata", version = "0.1.0", features = ["parse"] }
cargo-util = { path = "crates/cargo-util", version = "0.2.1" }
crates-io = { path = "crates/crates-io", version = "0.34.0" }
curl = { version = "0.4.43", features = ["http2"] }
Expand Down Expand Up @@ -73,11 +74,6 @@ itertools = "0.10.0"
# for more information.
rustc-workspace-hack = "1.0.0"

[target.'cfg(target_os = "linux")'.dependencies]
cargo-subcommand-metadata = { path = "crates/cargo-subcommand-metadata", version = "0.1.0" }
memmap = "0.7"
object = "0.28"

[target.'cfg(windows)'.dependencies]
fwdansi = "1.1.0"

Expand Down
7 changes: 7 additions & 0 deletions crates/cargo-subcommand-metadata/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,10 @@ edition = "2021"
license = "MIT OR Apache-2.0"
repository = "https://github.com/rust-lang/cargo"
description = "Embed metadata into a Cargo subcommand, so that `cargo --list` can show a description of the subcommand"

[target.'cfg(target_os = "linux")'.dependencies]
memmap = { version = "0.7", optional = true }
object = { version = "0.28", optional = true }

[features]
parse = ["memmap", "object"]
21 changes: 11 additions & 10 deletions crates/cargo-subcommand-metadata/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
#[cfg(feature = "parse")]
pub mod parse;

/// Cargo's name for the purpose of ELF notes.
///
/// The `name` field of an ELF note is designated to hold the entry's "owner" or
Expand Down Expand Up @@ -82,16 +85,14 @@ macro_rules! description {

#[used]
#[link_section = ".note.cargo.subcommand"]
static ELF_NOTE: ElfNote = {
ElfNote {
namesz: $crate::ELF_NOTE_NAME.len() as u32 + 1,
descsz: CARGO_SUBCOMMAND_DESCRIPTION.len() as u32,
ty: $crate::ElfNoteType::Description,
name: unsafe { *$crate::ELF_NOTE_NAME.as_ptr().cast() },
name_padding: $crate::private::padding(),
desc: unsafe { *CARGO_SUBCOMMAND_DESCRIPTION.as_ptr().cast() },
desc_padding: $crate::private::padding(),
}
static ELF_NOTE: ElfNote = ElfNote {
namesz: $crate::ELF_NOTE_NAME.len() as u32 + 1,
descsz: CARGO_SUBCOMMAND_DESCRIPTION.len() as u32,
ty: $crate::ElfNoteType::Description,
name: unsafe { *$crate::ELF_NOTE_NAME.as_ptr().cast() },
name_padding: $crate::private::padding(),
desc: unsafe { *CARGO_SUBCOMMAND_DESCRIPTION.as_ptr().cast() },
desc_padding: $crate::private::padding(),
};
};
};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::path::Path;

pub(crate) fn description(path: &Path) -> Option<String> {
pub fn description(path: &Path) -> Option<String> {
implementation::description(path)
}

Expand Down Expand Up @@ -29,9 +29,8 @@ mod implementation {
if section_header.name(endian, string_table).ok() == Some(b".note.cargo.subcommand") {
if let Ok(Some(mut notes)) = section_header.notes(endian, data) {
while let Ok(Some(note)) = notes.next() {
if note.name() == cargo_subcommand_metadata::ELF_NOTE_NAME.as_bytes()
&& note.n_type(endian)
== cargo_subcommand_metadata::ElfNoteType::Description as u32
if note.name() == crate::ELF_NOTE_NAME.as_bytes()
&& note.n_type(endian) == crate::ElfNoteType::Description as u32
{
if description.is_some() {
return None;
Expand Down
4 changes: 2 additions & 2 deletions src/bin/cargo/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use anyhow::anyhow;
use cargo::core::shell::Shell;
use cargo::core::{features, CliUnstable};
use cargo::{self, drop_print, drop_println, CliResult, Config};
use cargo_subcommand_metadata as subcommand_metadata;
use clap::{Arg, ArgMatches};
use itertools::Itertools;
use std::collections::HashMap;
Expand All @@ -12,7 +13,6 @@ use std::fmt::Write;
use super::commands;
use super::list_commands;
use crate::command_prelude::*;
use crate::subcommand_metadata;
use cargo::core::features::HIDDEN;

lazy_static::lazy_static! {
Expand Down Expand Up @@ -122,7 +122,7 @@ Run with 'cargo -Z [FLAG] [COMMAND]'",
CommandInfo::External { path } => {
if let Some(desc) = known_external_desc {
drop_println!(config, " {:<20} {}", name, desc);
} else if let Some(desc) = subcommand_metadata::description(&path) {
} else if let Some(desc) = subcommand_metadata::parse::description(&path) {
drop_println!(config, " {:<20} {}", name, desc);
} else if is_verbose {
drop_println!(config, " {:<20} {}", name, path.display());
Expand Down
1 change: 0 additions & 1 deletion src/bin/cargo/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ use std::path::{Path, PathBuf};

mod cli;
mod commands;
mod subcommand_metadata;

use crate::command_prelude::*;

Expand Down

0 comments on commit 8b6c7b7

Please sign in to comment.