Skip to content

Commit

Permalink
[#604] Use the libc crate
Browse files Browse the repository at this point in the history
  • Loading branch information
elBoberido committed Feb 2, 2025
1 parent c7d8694 commit 72ffd54
Show file tree
Hide file tree
Showing 46 changed files with 677 additions and 671 deletions.
38 changes: 21 additions & 17 deletions Cargo.Bazel.lock
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"checksum": "904dcb8a11d275446a0883d509f57dc79d98a328977a4d37fb7915476bec2922",
"checksum": "8a4cb983bd3e3b39bbdffe7693e509356851eec3e62381640c88afa0dc207023",
"crates": {
"addr2line 0.24.2": {
"name": "addr2line",
Expand Down Expand Up @@ -491,7 +491,7 @@
"target": "addr2line"
},
{
"id": "libc 0.2.159",
"id": "libc 0.2.169",
"target": "libc"
},
{
Expand Down Expand Up @@ -1371,7 +1371,7 @@
"target": "glob"
},
{
"id": "libc 0.2.159",
"id": "libc 0.2.169",
"target": "libc"
},
{
Expand Down Expand Up @@ -1715,7 +1715,7 @@
"deps": {
"common": [
{
"id": "libc 0.2.159",
"id": "libc 0.2.169",
"target": "libc"
},
{
Expand Down Expand Up @@ -1967,19 +1967,19 @@
"selects": {
"cfg(target_os = \"hermit\")": [
{
"id": "libc 0.2.159",
"id": "libc 0.2.169",
"target": "libc"
}
],
"cfg(target_os = \"wasi\")": [
{
"id": "libc 0.2.159",
"id": "libc 0.2.169",
"target": "libc"
}
],
"cfg(unix)": [
{
"id": "libc 0.2.159",
"id": "libc 0.2.169",
"target": "libc"
}
],
Expand Down Expand Up @@ -2156,7 +2156,7 @@
],
"cfg(unix)": [
{
"id": "libc 0.2.159",
"id": "libc 0.2.169",
"target": "libc"
}
]
Expand Down Expand Up @@ -3268,6 +3268,10 @@
{
"id": "lazy_static 1.5.0",
"target": "lazy_static"
},
{
"id": "libc 0.2.169",
"target": "libc"
}
],
"selects": {
Expand Down Expand Up @@ -3525,13 +3529,13 @@
},
"license": "MIT/Apache-2.0"
},
"libc 0.2.159": {
"libc 0.2.169": {
"name": "libc",
"version": "0.2.159",
"version": "0.2.169",
"repository": {
"Http": {
"url": "https://static.crates.io/crates/libc/0.2.159/download",
"sha256": "561d97a539a36e26a9a5fad1ea11a3039a67714694aaa379433e580854bc3dc5"
"url": "https://static.crates.io/crates/libc/0.2.169/download",
"sha256": "b5aba8db14291edd000dfcc4d620c7ebfb122c613afb886ca8803fa4e128a20a"
}
},
"targets": [
Expand Down Expand Up @@ -3621,14 +3625,14 @@
"deps": {
"common": [
{
"id": "libc 0.2.159",
"id": "libc 0.2.169",
"target": "build_script_build"
}
],
"selects": {}
},
"edition": "2015",
"version": "0.2.159"
"edition": "2021",
"version": "0.2.169"
},
"build_script_attrs": {
"data_glob": [
Expand Down Expand Up @@ -4801,7 +4805,7 @@
"alias": "libc_errno"
},
{
"id": "libc 0.2.159",
"id": "libc 0.2.169",
"target": "libc"
}
],
Expand Down Expand Up @@ -5623,7 +5627,7 @@
"selects": {
"cfg(unix)": [
{
"id": "libc 0.2.159",
"id": "libc 0.2.169",
"target": "libc"
}
],
Expand Down
5 changes: 3 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ colored = { version = "2.1" }
generic-tests = { version = "0.1.2" }
human-panic = { version = "1.2.3" }
lazy_static = { version = "1.4.0" }
libc = { version = "0.2.169" }
log = { version = "0.4.21" }
once_cell = { version = "1.19.0" }
ouroboros = { version = "0.18.4" }
Expand Down
8 changes: 8 additions & 0 deletions iceoryx2-bb/posix/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,14 @@ repository = { workspace = true }
rust-version = { workspace = true }
version = { workspace = true }

[features]
# Use https://crates.io/crates/libc for the platform abstraction. This simplifies
# cross-compilation since bindgen is not required anymore. Unfortunately, the libc crate
# does not offer all the functionality iceoryx2 requires on all of the supported
# platforms. Therefore, only a subset of the supported platforms will work with this
# feature flag.
libc_platform = ["iceoryx2-pal-posix/libc_platform"]

[dependencies]
iceoryx2-bb-container = { workspace = true }
iceoryx2-bb-system-types = { workspace = true }
Expand Down
9 changes: 9 additions & 0 deletions iceoryx2-pal/posix/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,14 @@ repository = { workspace = true }
rust-version = { workspace = true }
version = { workspace = true }

[features]
# Use https://crates.io/crates/libc for the platform abstraction. This simplifies
# cross-compilation since bindgen is not required anymore. Unfortunately, the libc crate
# does not offer all the functionality iceoryx2 requires on all of the supported
# platforms. Therefore, only a subset of the supported platforms will work with this
# feature flag.
libc_platform = []

[build-dependencies]
cc = { workspace = true }
bindgen = { workspace = true }
Expand All @@ -18,6 +26,7 @@ bindgen = { workspace = true }
iceoryx2-pal-concurrency-sync = { workspace = true }
iceoryx2-pal-configuration = { workspace = true }
lazy_static = { workspace = true }
libc = { workspace = true }

[target.'cfg(windows)'.dependencies]
windows-sys = { workspace = true }
Expand Down
16 changes: 10 additions & 6 deletions iceoryx2-pal/posix/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,18 @@
//
// SPDX-License-Identifier: Apache-2.0 OR MIT

extern crate bindgen;
extern crate cc;

use bindgen::*;
use std::env;
use std::path::PathBuf;
#[cfg(feature = "libc_platform")]
fn main() {}

#[cfg(not(feature = "libc_platform"))]
fn main() {
extern crate bindgen;
extern crate cc;

use bindgen::*;
use std::env;
use std::path::PathBuf;

#[cfg(any(target_os = "linux", target_os = "freebsd"))]
println!("cargo:rustc-link-lib=pthread");

Expand Down
6 changes: 0 additions & 6 deletions iceoryx2-pal/posix/src/freebsd/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,6 @@ pub const SO_PASSCRED: int = crate::internal::LOCAL_PEERCRED as _;
pub const SO_PEERCRED: int = crate::internal::LOCAL_PEERCRED as _;
pub const SCM_CREDENTIALS: int = crate::internal::SCM_CREDS as _;

pub const PTHREAD_PREFER_READER_NP: int = 0;
pub const PTHREAD_PREFER_WRITER_NP: int = 1;
pub const PTHREAD_PREFER_WRITER_NONRECURSIVE_NP: int = 2;

pub const PTHREAD_MUTEX_STALLED: int = crate::internal::PTHREAD_MUTEX_STALLED as _;
pub const PTHREAD_MUTEX_ROBUST: int = crate::internal::PTHREAD_MUTEX_ROBUST as _;
pub const PTHREAD_MUTEX_NORMAL: int = crate::internal::pthread_mutextype_PTHREAD_MUTEX_NORMAL as _;
Expand Down Expand Up @@ -103,8 +99,6 @@ pub const PTHREAD_PROCESS_SHARED: int = crate::internal::PTHREAD_PROCESS_SHARED
pub const PTHREAD_PRIO_NONE: int = crate::internal::PTHREAD_PRIO_NONE as _;
pub const PTHREAD_PRIO_INHERIT: int = crate::internal::PTHREAD_PRIO_INHERIT as _;
pub const PTHREAD_PRIO_PROTECT: int = crate::internal::PTHREAD_PRIO_PROTECT as _;
pub const PTHREAD_SCOPE_SYSTEM: int = crate::internal::PTHREAD_SCOPE_SYSTEM as _;
pub const PTHREAD_SCOPE_PROCESS: int = crate::internal::PTHREAD_SCOPE_PROCESS as _;

pub const RLIMIT_CPU: __rlim_t = 0;
pub const RLIMIT_FSIZE: __rlim_t = 1;
Expand Down
2 changes: 1 addition & 1 deletion iceoryx2-pal/posix/src/freebsd/fcntl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ pub unsafe fn open_with_mode(pathname: *const c_char, flags: int, mode: mode_t)
}

pub unsafe fn fstat(fd: int, buf: *mut stat_t) -> int {
let mut os_specific_buffer = crate::internal::stat::new();
let mut os_specific_buffer = native_stat_t::new();
match crate::internal::fstat(fd, &mut os_specific_buffer) {
0 => {
*buf = os_specific_buffer.into();
Expand Down
16 changes: 13 additions & 3 deletions iceoryx2-pal/posix/src/freebsd/pthread.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,9 @@ pub unsafe fn pthread_attr_setaffinity_np(
cpusetsize: size_t,
cpuset: *const cpu_set_t,
) -> int {
internal::pthread_attr_setaffinity_np(attr, cpusetsize, cpuset)
let cpuset = core::mem::transmute::<cpu_set_t, native_cpu_set_t>(*cpuset);

internal::pthread_attr_setaffinity_np(attr, cpusetsize, &cpuset)
}

pub unsafe fn pthread_create(
Expand Down Expand Up @@ -123,15 +125,23 @@ pub unsafe fn pthread_setaffinity_np(
cpusetsize: size_t,
cpuset: *const cpu_set_t,
) -> int {
internal::pthread_setaffinity_np(thread, cpusetsize, cpuset)
let cpuset = core::mem::transmute::<cpu_set_t, native_cpu_set_t>(*cpuset);

internal::pthread_setaffinity_np(thread, cpusetsize, &cpuset)
}

pub unsafe fn pthread_getaffinity_np(
thread: pthread_t,
cpusetsize: size_t,
cpuset: *mut cpu_set_t,
) -> int {
internal::pthread_getaffinity_np(thread, cpusetsize, cpuset)
let mut native_cpuset = native_cpu_set_t::new();

let ret_val = internal::pthread_getaffinity_np(thread, cpusetsize, &mut native_cpuset);

*cpuset = core::mem::transmute::<native_cpu_set_t, cpu_set_t>(native_cpuset);

ret_val
}

pub unsafe fn pthread_rwlockattr_init(attr: *mut pthread_rwlockattr_t) -> int {
Expand Down
2 changes: 1 addition & 1 deletion iceoryx2-pal/posix/src/freebsd/stat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use crate::posix::types::*;
use crate::posix::Struct;

pub unsafe fn stat(path: *const c_char, buf: *mut stat_t) -> int {
let mut os_specific_buffer = crate::internal::stat::new();
let mut os_specific_buffer = crate::native_stat_t::new();
match crate::internal::stat(path, &mut os_specific_buffer) {
0 => {
*buf = os_specific_buffer.into();
Expand Down
11 changes: 8 additions & 3 deletions iceoryx2-pal/posix/src/freebsd/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ pub type uint = crate::internal::uint;
pub type ushort = crate::internal::ushort;
pub type void = core::ffi::c_void;

pub(crate) type native_cpu_set_t = crate::internal::cpu_set_t;
impl Struct for native_cpu_set_t {}

pub type sigset_t = crate::internal::sigset_t;
impl Struct for sigset_t {}

Expand Down Expand Up @@ -100,6 +103,9 @@ impl Struct for rlimit {}
pub type sched_param = crate::internal::sched_param;
impl Struct for sched_param {}

pub(crate) type native_stat_t = libc::stat;
impl Struct for native_stat_t {}

#[repr(C)]
pub struct stat_t {
pub st_dev: dev_t,
Expand All @@ -116,8 +122,8 @@ pub struct stat_t {
pub st_blksize: blksize_t,
pub st_blocks: blkcnt_t,
}
impl From<crate::internal::stat> for stat_t {
fn from(value: crate::internal::stat) -> Self {
impl From<cnative_stat_t> for stat_t {
fn from(value: native_stat_t) -> Self {
stat_t {
st_dev: value.st_dev,
st_ino: value.st_ino,
Expand All @@ -136,7 +142,6 @@ impl From<crate::internal::stat> for stat_t {
}
}
impl Struct for stat_t {}
impl Struct for crate::internal::stat {}

pub type timespec = crate::internal::timespec;
impl Struct for timespec {}
Expand Down
25 changes: 16 additions & 9 deletions iceoryx2-pal/posix/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#![warn(clippy::std_instead_of_alloc)]
#![warn(clippy::std_instead_of_core)]

#[cfg(not(feature = "libc_platform"))]
pub(crate) mod internal {
#![allow(non_upper_case_globals)]
#![allow(non_camel_case_types)]
Expand All @@ -32,13 +33,16 @@ pub(crate) mod internal {
pub const ESUCCES: u32 = 0;
}

#[cfg(target_os = "freebsd")]
#[cfg(feature = "libc_platform")]
mod libc;

#[cfg(all(target_os = "freebsd", not(feature = "libc_platform")))]
mod freebsd;
#[cfg(target_os = "linux")]
#[cfg(all(target_os = "linux", not(feature = "libc_platform")))]
mod linux;
#[cfg(target_os = "macos")]
#[cfg(all(target_os = "macos", not(feature = "libc_platform")))]
mod macos;
#[cfg(target_os = "windows")]
#[cfg(all(target_os = "windows", not(feature = "libc_platform")))]
mod windows;

#[cfg(not(target_os = "windows"))]
Expand All @@ -49,13 +53,16 @@ use scandir::*;
pub mod posix {
#![allow(dead_code)]

#[cfg(target_os = "freebsd")]
#[cfg(feature = "libc_platform")]
pub use crate::libc::*;

#[cfg(all(target_os = "freebsd", not(feature = "libc_platform")))]
pub use crate::freebsd::*;
#[cfg(target_os = "linux")]
#[cfg(all(target_os = "linux", not(feature = "libc_platform")))]
pub use crate::linux::*;
#[cfg(target_os = "macos")]
#[cfg(all(target_os = "macos", not(feature = "libc_platform")))]
pub use crate::macos::*;
#[cfg(target_os = "windows")]
#[cfg(all(target_os = "windows", not(feature = "libc_platform")))]
pub use crate::windows::*;

pub trait Struct: Sized {
Expand Down Expand Up @@ -116,7 +123,7 @@ pub mod posix {
}
}

#[cfg(target_os = "windows")]
#[cfg(all(target_os = "windows", not(feature = "libc_platform")))]
pub(crate) mod win_internal {
#![allow(dead_code)]
use std::os::windows::prelude::OsStrExt;
Expand Down
Loading

0 comments on commit 72ffd54

Please sign in to comment.