arch: add support for building on Windows

This commit is contained in:
Alex Lyon 2018-03-04 17:58:16 -08:00 committed by Roy Ivy III
parent 3cec199da1
commit f575b8f4f2
3 changed files with 87 additions and 40 deletions

View file

@ -8,6 +8,7 @@ getopts = "0.2.14"
time = { version = "0.1.38", optional = true }
data-encoding = { version = "^1.1", optional = true }
libc = { version = "0.2.34", optional = true }
winapi = { version = "0.3", features = ["sysinfoapi"], optional = true }
[features]
fs = ["libc"]
@ -20,7 +21,7 @@ process = ["libc"]
signals = []
entries = ["libc"]
wide = []
utsname = ["libc"]
utsname = ["libc", "winapi"]
default = ["fs", "libc", "utf8", "utsname", "encoding", "parse_time", "mode", "utmpx", "process", "entries", "signals", "wide"]
[lib]

View file

@ -1,5 +1,7 @@
#[cfg(feature = "libc")]
pub extern crate libc;
#[cfg(feature = "winapi")]
pub extern crate winapi;
#[macro_use]
mod macros;
@ -22,7 +24,7 @@ pub mod parse_time;
pub mod mode;
#[cfg(all(unix, not(target_os = "fuchsia"), feature = "utmpx"))]
pub mod utmpx;
#[cfg(all(unix, feature = "utsname"))]
#[cfg(feature = "utsname")]
pub mod utsname;
#[cfg(all(unix, feature = "entries"))]
pub mod entries;

View file

@ -6,47 +6,91 @@
// that was distributed with this source code.
//
use super::libc::{uname, utsname};
use ::std::mem;
use ::std::ffi::CStr;
use ::std::borrow::Cow;
pub use self::platform::*;
macro_rules! cstr2cow {
($v:expr) => (
unsafe { CStr::from_ptr($v.as_ref().as_ptr()).to_string_lossy() }
)
}
#[cfg(unix)]
mod platform {
use super::libc::{uname, utsname};
use ::std::mem;
use ::std::ffi::CStr;
use ::std::borrow::Cow;
use ::std::io;
pub struct Uname {
inner: utsname,
}
macro_rules! cstr2cow {
($v:expr) => (
unsafe { CStr::from_ptr($v.as_ref().as_ptr()).to_string_lossy() }
)
}
impl Uname {
pub fn new() -> Self {
unsafe {
let mut uts: utsname = mem::uninitialized();
uname(&mut uts);
Uname { inner: uts }
pub struct Uname {
inner: utsname,
}
impl Uname {
pub fn new() -> io::Result<Self> {
unsafe {
let mut uts: utsname = mem::uninitialized();
if uname(&mut uts) == 0 {
Ok(Uname { inner: uts })
} else {
Err(io::Error::last_os_error())
}
}
}
pub fn sysname(&self) -> Cow<str> {
cstr2cow!(self.inner.sysname)
}
pub fn nodename(&self) -> Cow<str> {
cstr2cow!(self.inner.nodename)
}
pub fn release(&self) -> Cow<str> {
cstr2cow!(self.inner.release)
}
pub fn version(&self) -> Cow<str> {
cstr2cow!(self.inner.version)
}
pub fn machine(&self) -> Cow<str> {
cstr2cow!(self.inner.machine)
}
}
pub fn sysname(&self) -> Cow<str> {
cstr2cow!(self.inner.sysname)
}
pub fn nodename(&self) -> Cow<str> {
cstr2cow!(self.inner.nodename)
}
pub fn release(&self) -> Cow<str> {
cstr2cow!(self.inner.release)
}
pub fn version(&self) -> Cow<str> {
cstr2cow!(self.inner.version)
}
pub fn machine(&self) -> Cow<str> {
cstr2cow!(self.inner.machine)
}
}
#[cfg(windows)]
mod platform {
use ::winapi::um::sysinfoapi::{SYSTEM_INFO, GetSystemInfo};
use ::winapi::um::winnt::*;
use ::std::mem;
use ::std::borrow::Cow;
use ::std::io;
pub struct Uname {
inner: SYSTEM_INFO
}
impl Uname {
pub fn new() -> io::Result<Uname> {
unsafe {
let mut info = mem::uninitialized();
GetSystemInfo(&mut info);
Ok(Uname { inner: info })
}
}
// FIXME: need to implement more architectures (e.g. ARM)
pub fn machine(&self) -> Cow<str> {
let arch = unsafe {
match self.inner.u.s().wProcessorArchitecture {
PROCESSOR_ARCHITECTURE_AMD64 => "x86_64",
PROCESSOR_ARCHITECTURE_INTEL => "x86",
_ => unimplemented!()
}
};
Cow::from(arch)
}
}
}