arch: add support for building on Windows

This commit is contained in:
Alex Lyon 2018-03-04 17:58:16 -08:00
parent 3969b431a9
commit 5d241da7ca
6 changed files with 90 additions and 44 deletions

View file

@ -6,7 +6,6 @@ build = "build.rs"
[features]
unix = [
"arch",
"chgrp",
"chmod",
"chown",
@ -44,7 +43,6 @@ unix = [
# all utilities in that feature.
fuchsia = [
# unix utilities
"arch",
"chgrp",
"chmod",
"chown",
@ -67,6 +65,7 @@ fuchsia = [
"generic"
]
generic = [
"arch",
"cat",
"hashsum",
"join",

View file

@ -19,7 +19,7 @@ static LONG_HELP: &'static str = "";
pub fn uumain(args: Vec<String>) -> i32 {
new_coreopts!(SYNTAX, SUMMARY, LONG_HELP).parse(args);
let uts = Uname::new();
let uts = return_if_err!(1, Uname::new());
println!("{}", uts.machine().trim());
0
}

View file

@ -92,7 +92,7 @@ pub fn uumain(args: Vec<String>) -> i32 {
.get_matches_from(&args);
let argc = args.len();
let uname = Uname::new();
let uname = return_if_err!(1, Uname::new());
let mut output = String::new();
if matches.is_present(OPT_KERNELNAME) || matches.is_present(OPT_ALL) || argc == 1 {

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)
#[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
}
pub fn nodename(&self) -> Cow<str> {
cstr2cow!(self.inner.nodename)
}
impl Uname {
pub fn new() -> io::Result<Uname> {
unsafe {
let mut info = mem::uninitialized();
GetSystemInfo(&mut info);
Ok(Uname { inner: info })
}
}
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)
// 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)
}
}
}