From e9d6060d2ba5d74afd6811361d7f29a2c4892f42 Mon Sep 17 00:00:00 2001 From: Hanif Ariffin Date: Sun, 17 Nov 2024 21:37:17 +0800 Subject: [PATCH 1/2] Run fmt, update to 2021, update libc Signed-off-by: Hanif Ariffin --- Cargo.toml | 3 ++- src/ffi.rs | 4 ++-- src/lib.rs | 22 ++++++++++++++-------- src/main.rs | 21 ++++++++++++--------- 4 files changed, 30 insertions(+), 20 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 5f50009..adca6c4 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,6 +10,7 @@ repository = "https://github.com/zsiciarz/rust-cpuid" readme = "README.md" keywords = ["cpuid", "cpu", "hardware"] license = "MIT" +edition = "2021" [lib] name = "cpuid" @@ -19,7 +20,7 @@ name = "cpuid" doc = false [dependencies] -libc = "~0.2" +libc = "0.2" [features] unstable = [] diff --git a/src/ffi.rs b/src/ffi.rs index 2ea7f5f..01f494d 100644 --- a/src/ffi.rs +++ b/src/ffi.rs @@ -1,4 +1,4 @@ -use libc::{c_int, c_char, uint8_t, uint32_t, int32_t}; +use libc::{c_char, c_int, int32_t, uint32_t, uint8_t}; pub const MAX_CPUID_LEVEL: usize = 32; pub const MAX_EXT_CPUID_LEVEL: usize = 32; @@ -47,7 +47,7 @@ pub struct cpu_id_t { } #[link(name = "cpuid")] -extern { +extern "C" { pub fn cpuid_present() -> c_int; pub fn cpuid_lib_version() -> *const c_char; pub fn cpuid_error() -> *const c_char; diff --git a/src/lib.rs b/src/lib.rs index 4f728dc..b547991 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -203,7 +203,10 @@ pub fn version() -> String { unsafe { let ptr = ffi::cpuid_lib_version(); let bytes = CStr::from_ptr(ptr).to_bytes(); - str::from_utf8(bytes).ok().expect("Invalid UTF8 string").to_string() + str::from_utf8(bytes) + .ok() + .expect("Invalid UTF8 string") + .to_string() } } @@ -212,7 +215,10 @@ pub fn error() -> String { unsafe { let ptr = ffi::cpuid_error(); let bytes = CStr::from_ptr(ptr).to_bytes(); - str::from_utf8(bytes).ok().expect("Invalid UTF8 string").to_string() + str::from_utf8(bytes) + .ok() + .expect("Invalid UTF8 string") + .to_string() } } @@ -235,14 +241,14 @@ pub fn identify() -> Result { } else { Ok(CpuInfo { vendor: String::from_utf8(data.vendor_str.iter().map(|&x| x as u8).collect()) - .ok() - .expect("Invalid vendor string"), + .ok() + .expect("Invalid vendor string"), brand: String::from_utf8(data.brand_str.iter().map(|&x| x as u8).collect()) - .ok() - .expect("Invalid brand string"), + .ok() + .expect("Invalid brand string"), codename: String::from_utf8(data.cpu_codename.iter().map(|&x| x as u8).collect()) - .ok() - .expect("Invalid codename string"), + .ok() + .expect("Invalid codename string"), num_cores: data.num_cores, num_logical_cpus: data.num_logical_cpus, total_logical_cpus: data.total_logical_cpus, diff --git a/src/main.rs b/src/main.rs index 2831f9a..d2735d8 100644 --- a/src/main.rs +++ b/src/main.rs @@ -8,15 +8,18 @@ fn main() { Ok(info) => { println!("Found: {} CPU, model: {}", info.vendor, info.codename); println!("The full brand string is: {}", info.brand); - println!("The processor has {} cores and {} logical processors", - info.num_cores, - info.num_logical_cpus); - println!("Hardware AES support: {}", - if info.has_feature(cpuid::CpuFeature::AES) { - "yes" - } else { - "no" - }); + println!( + "The processor has {} cores and {} logical processors", + info.num_cores, info.num_logical_cpus + ); + println!( + "Hardware AES support: {}", + if info.has_feature(cpuid::CpuFeature::AES) { + "yes" + } else { + "no" + } + ); } Err(err) => println!("cpuid error: {}", err), } From 999a9bd2dd11d265fe4cb6cca55ebda4ab19714d Mon Sep 17 00:00:00 2001 From: Hanif Ariffin Date: Sun, 17 Nov 2024 21:36:24 +0800 Subject: [PATCH 2/2] Use bindgen Signed-off-by: Hanif Ariffin --- Cargo.toml | 3 +++ build.rs | 16 ++++++++++++++ src/ffi.rs | 61 +++++------------------------------------------------ src/lib.rs | 15 ++++++------- src/main.rs | 1 - wrapper.h | 3 +++ 6 files changed, 34 insertions(+), 65 deletions(-) create mode 100644 build.rs create mode 100644 wrapper.h diff --git a/Cargo.toml b/Cargo.toml index adca6c4..92ae555 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -19,6 +19,9 @@ name = "cpuid" name = "cpuid" doc = false +[build-dependencies] +bindgen = "0.65.1" + [dependencies] libc = "0.2" diff --git a/build.rs b/build.rs new file mode 100644 index 0000000..f398d43 --- /dev/null +++ b/build.rs @@ -0,0 +1,16 @@ +use std::env; +use std::path::PathBuf; + +fn main() { + println!("cargo:rustc-link-lib=cpuid"); + let bindings = bindgen::Builder::default() + .header("wrapper.h") + .parse_callbacks(Box::new(bindgen::CargoCallbacks)) + .generate() + .expect("Unable to generate bindings"); + let out_path = PathBuf::from(env::var("OUT_DIR").unwrap()); + // println!("bindings:{:#?}", bindings); + bindings + .write_to_file(out_path.join("bindings.rs")) + .expect("Couldn't write bindings!"); +} diff --git a/src/ffi.rs b/src/ffi.rs index 01f494d..cd503e4 100644 --- a/src/ffi.rs +++ b/src/ffi.rs @@ -1,57 +1,6 @@ -use libc::{c_char, c_int, int32_t, uint32_t, uint8_t}; +#![allow(non_upper_case_globals)] +#![allow(non_camel_case_types)] +#![allow(non_snake_case)] +#![allow(dead_code)] -pub const MAX_CPUID_LEVEL: usize = 32; -pub const MAX_EXT_CPUID_LEVEL: usize = 32; -pub const MAX_INTELFN4_LEVEL: usize = 4; -pub const MAX_INTELFN11_LEVEL: usize = 4; -pub const VENDOR_STR_MAX: usize = 16; -pub const BRAND_STR_MAX: usize = 64; -pub const CPU_FLAGS_MAX: usize = 128; -pub const CPU_HINTS_MAX: usize = 16; - -#[repr(C)] -pub struct cpu_raw_data_t { - pub basic_cpuid: [[uint32_t; 4]; MAX_CPUID_LEVEL], - pub ext_cpuid: [[uint32_t; 4]; MAX_EXT_CPUID_LEVEL], - pub intel_fn4: [[uint32_t; 4]; MAX_INTELFN4_LEVEL], - pub intel_fn11: [[uint32_t; 4]; MAX_INTELFN11_LEVEL], -} - -#[repr(C)] -pub struct cpu_id_t { - pub vendor_str: [c_char; VENDOR_STR_MAX], - pub brand_str: [c_char; BRAND_STR_MAX], - pub vendor: int32_t, - pub flags: [uint8_t; CPU_FLAGS_MAX], - pub family: int32_t, - pub model: int32_t, - pub stepping: int32_t, - pub ext_family: int32_t, - pub ext_model: int32_t, - pub num_cores: int32_t, - pub num_logical_cpus: int32_t, - pub total_logical_cpus: int32_t, - pub l1_data_cache: int32_t, - pub l1_instruction_cache: int32_t, - pub l2_cache: int32_t, - pub l3_cache: int32_t, - pub l1_assoc: int32_t, - pub l2_assoc: int32_t, - pub l3_assoc: int32_t, - pub l1_cacheline: int32_t, - pub l2_cacheline: int32_t, - pub l3_cacheline: int32_t, - pub cpu_codename: [c_char; 64], - pub sse_size: int32_t, - pub detection_hints: [uint8_t; CPU_HINTS_MAX], -} - -#[link(name = "cpuid")] -extern "C" { - pub fn cpuid_present() -> c_int; - pub fn cpuid_lib_version() -> *const c_char; - pub fn cpuid_error() -> *const c_char; - pub fn cpuid_get_raw_data(raw: *mut cpu_raw_data_t) -> c_int; - pub fn cpu_identify(raw: *mut cpu_raw_data_t, data: *mut cpu_id_t) -> c_int; - pub fn cpu_clock() -> c_int; -} +include!(concat!(env!("OUT_DIR"), "/bindings.rs")); diff --git a/src/lib.rs b/src/lib.rs index b547991..4f224e8 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -50,7 +50,6 @@ extern crate libc; use std::ffi::CStr; -use std::mem; use std::str; mod ffi; @@ -81,7 +80,7 @@ pub struct CpuInfo { pub l2_cache: Option, /// L3 cache size in kB. `Some(0)` if the CPU lacks L3 cache, `None` if it couldn't be determined. pub l3_cache: Option, - flags: [u8; ffi::CPU_FLAGS_MAX], + flags: [u8; ffi::CPU_FLAGS_MAX as usize], } /// CPU feature identifiers. @@ -195,16 +194,16 @@ impl CpuInfo { /// Checks if the CPUID instruction is present. pub fn is_present() -> bool { - unsafe { ffi::cpuid_present() == 1 } + let result = unsafe { ffi::cpuid_present() }; + result != 0 } /// Returns libcpuid version string. pub fn version() -> String { unsafe { let ptr = ffi::cpuid_lib_version(); - let bytes = CStr::from_ptr(ptr).to_bytes(); - str::from_utf8(bytes) - .ok() + CStr::from_ptr(ptr) + .to_str() .expect("Invalid UTF8 string") .to_string() } @@ -229,12 +228,12 @@ pub fn error() -> String { /// If libcpuid encounters an error, `identify` returns an `Err` with /// the error message inside. pub fn identify() -> Result { - let mut raw: ffi::cpu_raw_data_t = unsafe { mem::uninitialized() }; + let mut raw: ffi::cpu_raw_data_t = unsafe { std::mem::zeroed() }; let raw_result = unsafe { ffi::cpuid_get_raw_data(&mut raw) }; if raw_result != 0 { return Err(error()); } - let mut data: ffi::cpu_id_t = unsafe { mem::uninitialized() }; + let mut data: ffi::cpu_id_t = unsafe { std::mem::zeroed() }; let identify_result = unsafe { ffi::cpu_identify(&mut raw, &mut data) }; if identify_result != 0 { Err(error()) diff --git a/src/main.rs b/src/main.rs index d2735d8..bece93f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,5 @@ extern crate cpuid; -#[cfg(not(test))] fn main() { println!("cpuid is present: {}", cpuid::is_present()); println!("cpuid version: {}", cpuid::version()); diff --git a/wrapper.h b/wrapper.h new file mode 100644 index 0000000..f6b00c2 --- /dev/null +++ b/wrapper.h @@ -0,0 +1,3 @@ +#include +#include +#include