Skip to content

Commit

Permalink
Add generic_ctypes Cargo feature
Browse files Browse the repository at this point in the history
Now SGX and Switch can just use the generics
  • Loading branch information
josephlr committed Feb 7, 2019
1 parent 599bafe commit fa2d19f
Show file tree
Hide file tree
Showing 11 changed files with 205 additions and 40 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ use_std = []
align = []
rustc-dep-of-std = ['align', 'rustc-std-workspace-core']
extra_traits = ["align"]
generic_ctypes = []

[workspace]
members = ["libc-test"]
18 changes: 17 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ libc = { version = "0.2", features = ["align"] }
```

All structs implemented by the libc crate have the `Copy` and `Clone` traits
implemented for them. The additional traits of `Debug, `Eq`, `Hash`, and
implemented for them. The additional traits of `Debug`, `Eq`, `Hash`, and
`PartialEq` can be enabled with the *extra_traits* feature (requires Rust 1.25
or newer):

Expand All @@ -54,6 +54,22 @@ or newer):
libc = { version = "0.2", features = ["extra_traits"] }
```

A large number of targets are supported by libc; however, some unsupported or
custom targets just need libc's basic types (`c_int`, `size_t`, etc...) for
linking to C code. These types can be enabled for _any_ target with the
*generic_ctypes* feature:

```toml
[dependencies.libc]
version = "0.2"
default-features = false
features = ["generic_ctypes"]
```

Note that if you target has strange definitions for C types, *generic_ctypes*
will not necessarily generate the correct types, as the generation process is
based only on your CPU architecture and pointer size.

## What is libc?

The primary purpose of this crate is to provide all of the definitions necessary
Expand Down
16 changes: 16 additions & 0 deletions src/cloudabi/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
use dox::Option;

pub type c_short = i16;
pub type c_ushort = u16;
pub type c_int = i32;
pub type c_uint = u32;
pub type c_longlong = i64;
pub type c_ulonglong = u64;
pub type intmax_t = i64;
pub type uintmax_t = u64;

pub type size_t = usize;
pub type ptrdiff_t = isize;
pub type ssize_t = isize;

pub type in_addr_t = u32;
pub type in_port_t = u16;
pub type pthread_key_t = usize;
Expand Down Expand Up @@ -58,6 +71,9 @@ s! {
}
}

pub const INT_MIN: c_int = -2147483648;
pub const INT_MAX: c_int = 2147483647;

pub const _SC_NPROCESSORS_ONLN: ::c_int = 52;
pub const _SC_PAGESIZE: ::c_int = 54;

Expand Down
16 changes: 16 additions & 0 deletions src/fuchsia/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,19 @@ use dox::{mem, Option};

// PUB_TYPE

pub type c_short = i16;
pub type c_ushort = u16;
pub type c_int = i32;
pub type c_uint = u32;
pub type c_longlong = i64;
pub type c_ulonglong = u64;
pub type intmax_t = i64;
pub type uintmax_t = u64;

pub type size_t = usize;
pub type ptrdiff_t = isize;
pub type ssize_t = isize;

pub type pid_t = i32;
pub type uid_t = u32;
pub type gid_t = u32;
Expand Down Expand Up @@ -1069,6 +1082,9 @@ s! {

// PUB_CONST

pub const INT_MIN: c_int = -2147483648;
pub const INT_MAX: c_int = 2147483647;

pub const SIG_DFL: sighandler_t = 0 as sighandler_t;
pub const SIG_IGN: sighandler_t = 1 as sighandler_t;
pub const SIG_ERR: sighandler_t = !0 as sighandler_t;
Expand Down
100 changes: 100 additions & 0 deletions src/generic.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
//! This module attempts to attempts to give reasonable definitions for most
//! basic C types. Note that these are essentially educated guesses and are NOT
//! guaranteed to match the types produced by your C compiler.
// Per the C11 specification, these type definitions are not guaranteed to be
// correct for all platforms. However, virtually all platforms use these
// definitions, including all targets supported by rustc.
pub type c_short = i16;
pub type c_ushort = u16;
pub type c_int = i32;
pub type c_uint = u32;
pub type c_longlong = i64;
pub type c_ulonglong = u64;
pub type intmax_t = i64;
pub type uintmax_t = u64;
pub type size_t = usize;
pub type ptrdiff_t = isize;
pub type ssize_t = isize;

pub const INT_MIN: c_int = -2147483648;
pub const INT_MAX: c_int = 2147483647;

// There are two ways that platforms (in practice) differ in their C types:
// - chars can either be signed or unsigned
// - longs can either be 32-bit or 64-bit

// Whether chars default to signed or unsigned is primarily determined by
// architecture (windows is the main exception here).
pub use chars::*;
#[cfg(any(
target_arch = "aarch64",
target_arch = "arm",
target_arch = "armebv7r",
target_arch = "armv5te",
target_arch = "armv7",
target_arch = "armv7r",
target_arch = "armv7s",
target_arch = "asmjs",
target_arch = "wasm32",
target_arch = "wasm64",
target_arch = "powerpc",
target_arch = "powerpc64",
target_arch = "powerpc64le",
target_arch = "s390x",
target_arch = "thumbv6",
target_arch = "thumbv6m",
target_arch = "thummv7",
target_arch = "thumbv7em",
target_arch = "thumbv7m",
target_arch = "thumbv7neon",
target_arch = "tummbv8",
target_arch = "thumbv8m",
target_arch = "thumbv8m.main"
))]
mod chars {
pub type c_int = u8;
pub type wchar_t = u32;
}

#[cfg(any(
target_arch = "i386",
target_arch = "i586",
target_arch = "i686",
target_arch = "x86",
target_arch = "x86_64",
target_arch = "mips",
target_arch = "mips64",
target_arch = "mips64el",
target_arch = "mipsel",
target_arch = "nvptx",
target_arch = "nvptx64",
target_arch = "sparc64",
target_arch = "sparcv9",
target_arch = "riscv64",
target_arch = "riscv32",
target_arch = "riscv32imac",
target_arch = "riscv32imc"
))]
mod chars {
pub type c_int = i8;
pub type wchar_t = i32;
}

// On all platforms but Windows, longs are the same size as a pointer.
pub use long::*;
#[cfg(target_pointer_width = "64")]
mod long {
pub type c_long = i64;
pub type c_ulong = u64;
}

#[cfg(any(target_pointer_width = "16", target_pointer_width = "32"))]
mod long {
pub type c_long = i32;
pub type c_ulong = u32;
}

// POSIX specifications make no guarantees that off_t == long int, but this is
// what GNU and others always do.
pub type off_t = ::c_long;
35 changes: 8 additions & 27 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ mod macros;

mod dox;

// C types that are always the same Rust type
// Per the Rust and C11 specs, these C types will always have these definitions.
pub type int8_t = i8;
pub type int16_t = i16;
pub type int32_t = i32;
Expand All @@ -144,28 +144,12 @@ pub type uint8_t = u8;
pub type uint16_t = u16;
pub type uint32_t = u32;
pub type uint64_t = u64;

pub type intptr_t = isize;
pub type uintptr_t = usize;
pub type c_schar = i8;
pub type c_uchar = u8;
pub type c_short = i16;
pub type c_ushort = u16;
pub type c_int = i32;
pub type c_uint = u32;
pub type c_float = f32;
pub type c_double = f64;
pub type c_longlong = i64;
pub type c_ulonglong = u64;
pub type intmax_t = i64;
pub type uintmax_t = u64;

pub type size_t = usize;
pub type ptrdiff_t = isize;
pub type intptr_t = isize;
pub type uintptr_t = usize;
pub type ssize_t = isize;

pub const INT_MIN: c_int = -2147483648;
pub const INT_MAX: c_int = 2147483647;

cfg_if! {
if #[cfg(core_cvoid)] {
Expand Down Expand Up @@ -199,16 +183,13 @@ cfg_if! {
} else if #[cfg(target_os = "fuchsia")] {
mod fuchsia;
pub use fuchsia::*;
} else if #[cfg(target_os = "switch")] {
mod switch;
pub use switch::*;
} else if #[cfg(unix)] {
mod unix;
pub use unix::*;
} else if #[cfg(target_env = "sgx")] {
mod sgx;
pub use sgx::*;
} else {
// non-supported targets: empty...
} else {
// Some targets only need the generic C types (and don't need functions)
#[cfg(any(feature = "generic_ctypes", target_env = "sgx", target_os = "switch"))]
mod generic;
pub use generic::*;
}
}
16 changes: 16 additions & 0 deletions src/redox/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
pub type c_short = i16;
pub type c_ushort = u16;
pub type c_int = i32;
pub type c_uint = u32;
pub type c_longlong = i64;
pub type c_ulonglong = u64;
pub type intmax_t = i64;
pub type uintmax_t = u64;

pub type size_t = usize;
pub type ptrdiff_t = isize;
pub type ssize_t = isize;

pub type c_char = i8;
pub type c_long = i64;
pub type c_ulong = u64;
Expand Down Expand Up @@ -73,6 +86,9 @@ s! {
}
}

pub const INT_MIN: ::c_int = -2147483648;
pub const INT_MAX: ::c_int = 2147483647;

pub const STDIN_FILENO: ::c_int = 0;
pub const STDOUT_FILENO: ::c_int = 1;
pub const STDERR_FILENO: ::c_int = 2;
Expand Down
5 changes: 0 additions & 5 deletions src/sgx.rs

This file was deleted.

7 changes: 0 additions & 7 deletions src/switch.rs

This file was deleted.

16 changes: 16 additions & 0 deletions src/unix/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,19 @@
use dox::Option;

pub type c_short = i16;
pub type c_ushort = u16;
pub type c_int = i32;
pub type c_uint = u32;
pub type c_longlong = i64;
pub type c_ulonglong = u64;
pub type intmax_t = i64;
pub type uintmax_t = u64;

pub type size_t = usize;
pub type ptrdiff_t = isize;
pub type ssize_t = isize;

pub type pid_t = i32;
pub type uid_t = u32;
pub type gid_t = u32;
Expand Down Expand Up @@ -198,6 +211,9 @@ s! {
}
}

pub const INT_MIN: ::c_int = -2147483648;
pub const INT_MAX: ::c_int = 2147483647;

pub const SIG_DFL: sighandler_t = 0 as sighandler_t;
pub const SIG_IGN: sighandler_t = 1 as sighandler_t;
pub const SIG_ERR: sighandler_t = !0 as sighandler_t;
Expand Down
15 changes: 15 additions & 0 deletions src/windows/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
//! Windows CRT definitions
pub type c_short = i16;
pub type c_ushort = u16;
pub type c_int = i32;
pub type c_uint = u32;
pub type c_longlong = i64;
pub type c_ulonglong = u64;
pub type intmax_t = i64;
pub type uintmax_t = u64;

pub type size_t = usize;
pub type ptrdiff_t = isize;
pub type ssize_t = isize;
pub type sighandler_t = usize;

pub type c_char = i8;
Expand Down Expand Up @@ -80,6 +92,9 @@ s! {
}
}

pub const INT_MIN: ::c_int = -2147483648;
pub const INT_MAX: ::c_int = 2147483647;

pub const EXIT_FAILURE: ::c_int = 1;
pub const EXIT_SUCCESS: ::c_int = 0;
pub const RAND_MAX: ::c_int = 32767;
Expand Down

0 comments on commit fa2d19f

Please sign in to comment.