Re-export core::ffi::c_void if supported

This commit is contained in:
Isaac Woods 2018-09-17 19:33:52 +01:00
parent 1844a772b6
commit 79c80c4ec4
No known key found for this signature in database
GPG Key ID: 1FE6E6BADEC3C88E
4 changed files with 70 additions and 23 deletions

View File

@ -12,6 +12,7 @@ description = """
A library for types and bindings to native C functions often found in libc or
other common platform libraries.
"""
build = "build.rs"
[badges]
travis-ci = { repository = "rust-lang/libc" }

35
build.rs Normal file
View File

@ -0,0 +1,35 @@
use std::env;
use std::process::Command;
use std::str;
fn main() {
/*
* If `core::ffi::c_void` exists, libc can just re-export it. Otherwise, it
* must define an incompatible type to retain backwards-compatibility.
*/
if rustc_minor_version().expect("Failed to get rustc version") >= 31 {
println!("cargo:rustc-cfg=core_cvoid");
}
}
fn rustc_minor_version() -> Option<u32> {
macro_rules! otry {
($e:expr) => {
match $e {
Some(e) => e,
None => return None,
}
};
}
let rustc = otry!(env::var_os("RUSTC"));
let output = otry!(Command::new(rustc).arg("--version").output().ok());
let version = otry!(str::from_utf8(&output.stdout).ok());
let mut pieces = version.split('.');
if pieces.next() != Some("rustc 1") {
return None;
}
otry!(pieces.next()).parse().ok()
}

View File

@ -108,17 +108,22 @@ cfg_if! {
// On the Switch, we only define some useful universal types for
// convenience. Those can be found in the switch.rs file.
} else {
// Use repr(u8) as LLVM expects `void*` to be the same as `i8*` to help enable
// more optimization opportunities around it recognizing things like
// malloc/free.
#[repr(u8)]
pub enum c_void {
// Two dummy variants so the #[repr] attribute can be used.
#[doc(hidden)]
__variant1,
#[doc(hidden)]
__variant2,
cfg_if! {
if #[cfg(core_cvoid)] {
pub use core::ffi::c_void;
} else {
// Use repr(u8) as LLVM expects `void*` to be the same as `i8*` to help enable
// more optimization opportunities around it recognizing things like
// malloc/free.
#[repr(u8)]
pub enum c_void {
// Two dummy variants so the #[repr] attribute can be used.
#[doc(hidden)]
__variant1,
#[doc(hidden)]
__variant2,
}
}
}
pub type int8_t = i8;

View File

@ -1,17 +1,5 @@
//! Switch C type definitions
// Use repr(u8) as LLVM expects `void*` to be the same as `i8*` to help enable
// more optimization opportunities around it recognizing things like
// malloc/free.
#[repr(u8)]
pub enum c_void {
// Two dummy variants so the #[repr] attribute can be used.
#[doc(hidden)]
__variant1,
#[doc(hidden)]
__variant2,
}
pub type int8_t = i8;
pub type int16_t = i16;
pub type int32_t = i32;
@ -46,3 +34,21 @@ pub type c_char = u8;
pub type c_long = i64;
pub type c_ulong = u64;
pub type wchar_t = u32;
cfg_if! {
if #[cfg(core_cvoid)] {
pub use core::ffi::c_void;
} else {
// Use repr(u8) as LLVM expects `void*` to be the same as `i8*` to help
// enable more optimization opportunities around it recognizing things
// like malloc/free.
#[repr(u8)]
pub enum c_void {
// Two dummy variants so the #[repr] attribute can be used.
#[doc(hidden)]
__variant1,
#[doc(hidden)]
__variant2,
}
}
}