Skip to content

Commit

Permalink
add more information about the target
Browse files Browse the repository at this point in the history
  • Loading branch information
danielschemmel committed Jan 18, 2023
1 parent b7d6aa7 commit aee0d6f
Show file tree
Hide file tree
Showing 14 changed files with 281 additions and 16 deletions.
5 changes: 3 additions & 2 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
{
"cSpell.words": [
"Deque",
"bincode",
"chrono",
"clippy",
"concat",
"datelike",
"deps",
"Deque",
"endianness",
"eval",
"flate",
"hasher",
Expand All @@ -17,4 +18,4 @@
"semver",
"serde"
]
}
}
1 change: 0 additions & 1 deletion build-info-build/src/build_script_options/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,5 @@ pub(crate) fn get_info() -> CompilerInfo {
commit_date,
channel,
host_triple: rustc_version.host,
target_triple: std::env::var("TARGET").unwrap_or_else(|_| "UNKNOWN".to_string()),
}
}
5 changes: 4 additions & 1 deletion build-info-build/src/build_script_options/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use super::BuildInfo;

mod compiler;
mod crate_info;
mod target;
mod timestamp;
mod version_control;

Expand Down Expand Up @@ -50,10 +51,11 @@ impl BuildScriptOptions {
};

let compiler = compiler::get_info();
let target = target::get_info();
let crate_info::Manifest {
crate_info,
workspace_root,
} = crate_info::read_manifest(&compiler.target_triple, self.collect_dependencies);
} = crate_info::read_manifest(&target.triple, self.collect_dependencies);
let version_control = version_control::get_info();

let timestamp = self.timestamp.unwrap_or_else(timestamp::get_timestamp);
Expand All @@ -63,6 +65,7 @@ impl BuildScriptOptions {
optimization_level,
crate_info,
compiler,
target,
version_control,
};

Expand Down
29 changes: 29 additions & 0 deletions build-info-build/src/build_script_options/target.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
use build_info_common::{CpuInfo, Endianness, TargetInfo};

pub(crate) fn get_info() -> TargetInfo {
TargetInfo {
triple: std::env::var("TARGET").unwrap_or_else(|_| "UNKNOWN".to_string()),
family: std::env::var("CARGO_CFG_TARGET_FAMILY").unwrap_or_else(|_| "UNKNOWN".to_string()),
os: std::env::var("CARGO_CFG_TARGET_OS").unwrap_or_else(|_| "UNKNOWN".to_string()),
cpu: CpuInfo {
arch: std::env::var("CARGO_CFG_TARGET_ARCH").unwrap_or_else(|_| "UNKNOWN".to_string()),
pointer_width: std::env::var("CARGO_CFG_TARGET_POINTER_WIDTH")
.expect("Could not read `CARGO_CFG_TARGET_POINTER_WIDTH`")
.parse()
.expect("Could not parse the target pointer width from `CARGO_CFG_TARGET_POINTER_WIDTH`"),
endianness: match std::env::var("CARGO_CFG_TARGET_ENDIAN") {
Ok(val) => match val.as_str() {
"little" => Endianness::Little,
"big" => Endianness::Big,
_ => panic!("Unknown endianness: {val:?}"),
},
Err(err) => panic!("Could not read `CARGO_CFG_TARGET_ENDIAN`: {err}"),
},
features: std::env::var("CARGO_CFG_TARGET_FEATURE")
.unwrap_or_default()
.split(',')
.map(|s| s.to_owned())
.collect(),
},
}
}
12 changes: 12 additions & 0 deletions build-info-common/src/display.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,18 @@ impl std::fmt::Display for crate::CrateInfo {
}
}

impl std::fmt::Display for crate::TargetInfo {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(f, "{}", self.triple)
}
}

impl std::fmt::Display for crate::CpuInfo {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(f, "{}", self.arch)
}
}

impl std::fmt::Display for crate::CompilerInfo {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(f, "rustc {}", self.version)?;
Expand Down
44 changes: 39 additions & 5 deletions build-info-common/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,13 @@ pub struct BuildInfo {
/// Information about the current crate
pub crate_info: CrateInfo,

/// Information about the compiler used.
/// Information about the target system
pub target: TargetInfo,

/// Information about the compiler used
pub compiler: CompilerInfo,

/// `Some` if the project is inside a check-out of a supported version control system.
/// `Some` if the project is inside a check-out of a supported version control system
pub version_control: Option<VersionControl>,
}

Expand Down Expand Up @@ -90,6 +93,40 @@ pub struct CrateInfo {
pub dependencies: Vec<CrateInfo>,
}

#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug)]
pub struct TargetInfo {
/// Identifies the target architecture for which the crate is being compiled
pub triple: String,
/// A generic description of the target, e.g., `"unix"` or `"wasm"`
pub family: String,
/// The target OS
pub os: String,
/// The target CPU
pub cpu: CpuInfo,
}

#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug)]
pub struct CpuInfo {
/// The CPU target architecture
pub arch: String,
/// The CPU pointer width
pub pointer_width: u64,
/// The CPU target endianness
pub endianness: Endianness,
/// List of CPU target features enabled
pub features: Vec<String>,
}

/// CPU Endianness
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Display, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug)]
pub enum Endianness {
Big,
Little,
}

/// `rustc` version and configuration
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug)]
Expand All @@ -108,9 +145,6 @@ pub struct CompilerInfo {

/// Identifies the host on which `rustc` was running
pub host_triple: String,

/// Identifies the target architecture for which the crate is being compiled
pub target_triple: String,
}

/// `rustc` distribution channel (some compiler features are only available on specific channels)
Expand Down
1 change: 1 addition & 0 deletions build-info-proc/src/format/value/build_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ impl Value for BuildInfo {
"profile" => Ok(Box::new(self.profile.clone())),
"optimization_level" => Ok(Box::new(self.optimization_level)),
"crate_info" => Ok(Box::new(self.crate_info.clone())),
"target" => Ok(Box::new(self.target.clone())),
"compiler" => Ok(Box::new(self.compiler.clone())),
"version_control" => Ok(Box::new(self.version_control.clone())),
_ => self.call_base(func, args),
Expand Down
1 change: 0 additions & 1 deletion build-info-proc/src/format/value/compiler_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ impl Value for CompilerInfo {
"commit_date" => Ok(Box::new(self.commit_date)),
"channel" => Ok(Box::new(self.channel)),
"host_triple" => Ok(Box::new(self.host_triple.clone())),
"target_triple" => Ok(Box::new(self.target_triple.clone())),
_ => self.call_base(func, args),
},
"to_string" => {
Expand Down
43 changes: 43 additions & 0 deletions build-info-proc/src/format/value/cpu_info.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
use std::any::Any;

use build_info_common::CpuInfo;
use num_bigint::BigInt;

use super::{as_arguments_0, as_field_name, FormatSpecifier, Type, Value, OP_FIELD_ACCESS};

impl Value for CpuInfo {
fn call(&self, func: &str, args: &[Box<dyn Value>]) -> anyhow::Result<Box<dyn Value>> {
match func {
OP_FIELD_ACCESS => match as_field_name(args) {
"arch" => Ok(Box::new(self.arch.clone())),
"pointer_width" => Ok(Box::new(BigInt::from(self.pointer_width))),
"endianness" => Ok(Box::new(self.endianness)),
"features" => Ok(Box::new(self.features.clone())),
_ => self.call_base(func, args),
},
"to_string" => {
as_arguments_0(args)?;
Ok(Box::new(self.to_string()))
}
_ => self.call_base(func, args),
}
}

fn get_type(&self) -> Type {
Type::CompilerInfo
}

fn as_any(&self) -> &dyn Any {
self
}

fn format(&self, buffer: &mut String, spec: FormatSpecifier) {
use std::fmt::Write;

match spec {
FormatSpecifier::Default => write!(buffer, "{self}").unwrap(),
FormatSpecifier::Debug => write!(buffer, "{self:?}").unwrap(),
FormatSpecifier::DebugAlt => write!(buffer, "{self:#?}").unwrap(),
}
}
}
35 changes: 35 additions & 0 deletions build-info-proc/src/format/value/endianness.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
use std::any::Any;

use build_info_common::Endianness;

use super::{as_arguments_0, FormatSpecifier, Type, Value};

impl Value for Endianness {
fn call(&self, func: &str, args: &[Box<dyn Value>]) -> anyhow::Result<Box<dyn Value>> {
match func {
"to_string" => {
as_arguments_0(args)?;
Ok(Box::new(self.to_string()))
}
_ => self.call_base(func, args),
}
}

fn get_type(&self) -> Type {
Type::CompilerChannel
}

fn as_any(&self) -> &dyn Any {
self
}

fn format(&self, buffer: &mut String, spec: FormatSpecifier) {
use std::fmt::Write;

match spec {
FormatSpecifier::Default => write!(buffer, "{self}").unwrap(),
FormatSpecifier::Debug => write!(buffer, "{self:?}").unwrap(),
FormatSpecifier::DebugAlt => write!(buffer, "{self:#?}").unwrap(),
}
}
}
3 changes: 3 additions & 0 deletions build-info-proc/src/format/value/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,12 @@ mod semver;
mod build_info;
mod compiler_channel;
mod compiler_info;
mod cpu_info;
mod crate_info;
mod endianness;
mod git_info;
mod optimization_level;
mod target_info;
mod version_control;

mod functions;
Expand Down
42 changes: 42 additions & 0 deletions build-info-proc/src/format/value/target_info.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
use std::any::Any;

use build_info_common::TargetInfo;

use super::{as_arguments_0, as_field_name, FormatSpecifier, Type, Value, OP_FIELD_ACCESS};

impl Value for TargetInfo {
fn call(&self, func: &str, args: &[Box<dyn Value>]) -> anyhow::Result<Box<dyn Value>> {
match func {
OP_FIELD_ACCESS => match as_field_name(args) {
"triple" => Ok(Box::new(self.triple.clone())),
"family" => Ok(Box::new(self.family.clone())),
"os" => Ok(Box::new(self.os.clone())),
"cpu" => Ok(Box::new(self.cpu.clone())),
_ => self.call_base(func, args),
},
"to_string" => {
as_arguments_0(args)?;
Ok(Box::new(self.to_string()))
}
_ => self.call_base(func, args),
}
}

fn get_type(&self) -> Type {
Type::CompilerInfo
}

fn as_any(&self) -> &dyn Any {
self
}

fn format(&self, buffer: &mut String, spec: FormatSpecifier) {
use std::fmt::Write;

match spec {
FormatSpecifier::Default => write!(buffer, "{self}").unwrap(),
FormatSpecifier::Debug => write!(buffer, "{self:?}").unwrap(),
FormatSpecifier::DebugAlt => write!(buffer, "{self:#?}").unwrap(),
}
}
}
Loading

0 comments on commit aee0d6f

Please sign in to comment.