Skip to content

Commit

Permalink
Make target-lexicon an optional dependency
Browse files Browse the repository at this point in the history
This commit should finish out gimli-rs#215 by removing the dependencies of the
`object` crate if it's built with few enough features. While hopefully
not exercised by most users this should be enough for the `backtrace`
crate to depend on `object` and not pull in any transitive dependencies.
One step closer to moving into libstd!

Closes gimli-rs#215
  • Loading branch information
alexcrichton committed May 7, 2020
1 parent c2df6df commit 8bdacd8
Show file tree
Hide file tree
Showing 10 changed files with 32 additions and 22 deletions.
2 changes: 2 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ script:
- cargo build --no-default-features --features read_core,write_core,macho
- cargo build --no-default-features --features read_core,pe
- cargo build --no-default-features --features read_core,wasm
- cargo build --no-default-features --features read_core,coff,elf,pe,wasm,macho
- cargo build --no-default-features --features read_core,coff,elf,pe,wasm,macho,architecture
- if [ "$TRAVIS_OS_NAME" = linux ] && [ "$TRAVIS_RUST_VERSION" = stable ]; then
rustup component add rustfmt;
cargo fmt -- --check;
Expand Down
5 changes: 3 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,18 @@ all-features = true
crc32fast = { version = "1.2", optional = true }
flate2 = { version = "1", optional = true }
indexmap = { version = "1.1", optional = true }
target-lexicon = { version = "0.10" }
target-lexicon = { version = "0.10", optional = true }
wasmparser = { version = "0.52", optional = true }

[dev-dependencies]
memmap = "0.7"

[features]
read_core = []
read = ["read_core", "coff", "elf", "macho", "pe", "wasm"]
read = ["read_core", "coff", "elf", "macho", "pe", "wasm", "architecture"]
write_core = ["crc32fast", "indexmap", "std"]
write = ["write_core", "coff", "elf", "macho"]
architecture = ["target-lexicon"]

std = []
compression = ["flate2", "std"]
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ extern crate alloc;
extern crate std;

// Re-export since these are used in public signatures.
#[cfg(feature = "architecture")]
pub use target_lexicon;

mod common;
Expand Down
8 changes: 5 additions & 3 deletions src/read/any.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#[cfg(feature = "compression")]
use alloc::borrow::Cow;
use alloc::fmt;
use target_lexicon::{Architecture, BinaryFormat};

#[cfg(feature = "coff")]
use crate::read::coff;
Expand Down Expand Up @@ -219,7 +218,9 @@ impl<'data> File<'data> {
}

/// Return the file format.
pub fn format(&self) -> BinaryFormat {
#[cfg(feature = "architecture")]
pub fn format(&self) -> target_lexicon::BinaryFormat {
use target_lexicon::BinaryFormat;
match self.inner {
#[cfg(feature = "coff")]
FileInternal::Coff(_) => BinaryFormat::Coff,
Expand Down Expand Up @@ -247,7 +248,8 @@ where
type SectionIterator = SectionIterator<'data, 'file>;
type SymbolIterator = SymbolIterator<'data, 'file>;

fn architecture(&self) -> Architecture {
#[cfg(feature = "architecture")]
fn architecture(&self) -> target_lexicon::Architecture {
with_inner!(self.inner, FileInternal, |x| x.architecture())
}

Expand Down
5 changes: 3 additions & 2 deletions src/read/coff/file.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use alloc::vec::Vec;
use core::str;
use target_lexicon::Architecture;

use crate::endian::LittleEndian as LE;
use crate::pe;
Expand Down Expand Up @@ -54,7 +53,9 @@ where
type SectionIterator = CoffSectionIterator<'data, 'file>;
type SymbolIterator = CoffSymbolIterator<'data, 'file>;

fn architecture(&self) -> Architecture {
#[cfg(feature = "architecture")]
fn architecture(&self) -> target_lexicon::Architecture {
use target_lexicon::Architecture;
match self.header.machine.get(LE) {
pe::IMAGE_FILE_MACHINE_I386 => Architecture::I386,
pe::IMAGE_FILE_MACHINE_AMD64 => Architecture::X86_64,
Expand Down
6 changes: 3 additions & 3 deletions src/read/elf/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ use alloc::vec::Vec;
use core::fmt::Debug;
use core::{mem, str};

use target_lexicon::{Aarch64Architecture, Architecture, ArmArchitecture};

use crate::elf;
use crate::endian::{self, Endian, RunTimeEndian, U32};
use crate::pod::{Bytes, Pod};
Expand Down Expand Up @@ -110,7 +108,9 @@ where
type SectionIterator = ElfSectionIterator<'data, 'file, Elf>;
type SymbolIterator = ElfSymbolIterator<'data, 'file, Elf>;

fn architecture(&self) -> Architecture {
#[cfg(feature = "architecture")]
fn architecture(&self) -> target_lexicon::Architecture {
use target_lexicon::{Aarch64Architecture, Architecture, ArmArchitecture};
match self.header.e_machine(self.endian) {
elf::EM_ARM => Architecture::Arm(ArmArchitecture::Arm),
elf::EM_AARCH64 => Architecture::Aarch64(Aarch64Architecture::Aarch64),
Expand Down
5 changes: 3 additions & 2 deletions src/read/macho/file.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use alloc::vec::Vec;
use core::fmt::Debug;
use core::{mem, str};
use target_lexicon::{Aarch64Architecture, Architecture, ArmArchitecture};

use crate::endian::{self, BigEndian, Endian, RunTimeEndian};
use crate::macho;
Expand Down Expand Up @@ -93,7 +92,9 @@ where
type SectionIterator = MachOSectionIterator<'data, 'file, Mach>;
type SymbolIterator = MachOSymbolIterator<'data, 'file, Mach>;

fn architecture(&self) -> Architecture {
#[cfg(feature = "architecture")]
fn architecture(&self) -> target_lexicon::Architecture {
use target_lexicon::{Aarch64Architecture, Architecture, ArmArchitecture};
match self.header.cputype(self.endian) {
macho::CPU_TYPE_ARM => Architecture::Arm(ArmArchitecture::Arm),
macho::CPU_TYPE_ARM64 => Architecture::Aarch64(Aarch64Architecture::Aarch64),
Expand Down
5 changes: 3 additions & 2 deletions src/read/pe/file.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use alloc::vec::Vec;
use core::fmt::Debug;
use core::{mem, str};
use target_lexicon::Architecture;

use crate::endian::LittleEndian as LE;
use crate::pe;
Expand Down Expand Up @@ -80,7 +79,9 @@ where
type SectionIterator = PeSectionIterator<'data, 'file, Pe>;
type SymbolIterator = CoffSymbolIterator<'data, 'file>;

fn architecture(&self) -> Architecture {
#[cfg(feature = "architecture")]
fn architecture(&self) -> target_lexicon::Architecture {
use target_lexicon::Architecture;
match self.nt_headers.file_header().machine.get(LE) {
// TODO: Arm/Arm64
pe::IMAGE_FILE_MACHINE_I386 => Architecture::I386,
Expand Down
11 changes: 6 additions & 5 deletions src/read/traits.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#[cfg(feature = "compression")]
use alloc::borrow::Cow;
use target_lexicon::{Architecture, Endianness};

use crate::read::{self, Result};
use crate::{
Expand All @@ -25,15 +24,17 @@ pub trait Object<'data, 'file>: read::private::Sealed {
type SymbolIterator: Iterator<Item = (SymbolIndex, Symbol<'data>)>;

/// Get the architecture type of the file.
fn architecture(&self) -> Architecture;
#[cfg(feature = "architecture")]
fn architecture(&self) -> target_lexicon::Architecture;

/// Get the endianness of the file.
#[inline]
fn endianness(&self) -> Endianness {
#[cfg(feature = "architecture")]
fn endianness(&self) -> target_lexicon::Endianness {
if self.is_little_endian() {
Endianness::Little
target_lexicon::Endianness::Little
} else {
Endianness::Big
target_lexicon::Endianness::Big
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/read/wasm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ use alloc::boxed::Box;
use alloc::vec::Vec;
use core::marker::PhantomData;
use core::{slice, str};
use target_lexicon::Architecture;
use wasmparser as wp;

use crate::read::{
Expand Down Expand Up @@ -308,8 +307,9 @@ where
type SymbolIterator = WasmSymbolIterator<'data, 'file>;

#[inline]
fn architecture(&self) -> Architecture {
Architecture::Wasm32
#[cfg(feature = "architecture")]
fn architecture(&self) -> target_lexicon::Architecture {
target_lexicon::Architecture::Wasm32
}

#[inline]
Expand Down

0 comments on commit 8bdacd8

Please sign in to comment.