Skip to content

Commit 8f23067

Browse files
committed
Use unions and const size_of only if the Rust version supports them
1 parent 61ff4ac commit 8f23067

File tree

6 files changed

+98
-51
lines changed

6 files changed

+98
-51
lines changed

.travis.yml

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,7 @@ matrix:
4949
- env: TARGET=sparc64-unknown-linux-gnu
5050
- env: TARGET=asmjs-unknown-emscripten
5151
- env: TARGET=wasm32-unknown-emscripten
52-
- env: TARGET=wasm32-unknown-unknown
53-
install: rustup target add $TARGET
54-
script: cargo build --no-default-features --target $TARGET --release
52+
- env: TARGET=wasm32-unknown-unknown BUILD_ONLY=1
5553

5654
allow_failures:
5755
# FIXME: https://github.com/rust-lang/libc/issues/1226
@@ -62,10 +60,16 @@ install:
6260
- rustup install beta
6361
- rustup install stable
6462
- rustup install 1.13.0
63+
- rustup install 1.19.0
64+
- rustup install 1.24.0
65+
- rustup install 1.30.0
6566
- rustup target add $TARGET || true
6667
- rustup target add $TARGET --toolchain beta || true
6768
- rustup target add $TARGET --toolchain stable || true
6869
- rustup target add $TARGET --toolchain 1.13.0 || true
70+
- rustup target add $TARGET --toolchain 1.19.0 || true
71+
- rustup target add $TARGET --toolchain 1.24.0 || true
72+
- rustup target add $TARGET --toolchain 1.30.0 || true
6973

7074
script:
7175
- cargo generate-lockfile --manifest-path libc-test/Cargo.toml

build.rs

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,22 @@ use std::process::Command;
33
use std::str;
44

55
fn main() {
6-
/*
7-
* If `core::ffi::c_void` exists, libc can just re-export it. Otherwise, it
8-
* must define an incompatible type to retain backwards-compatibility.
9-
*/
10-
if rustc_minor_version().expect("Failed to get rustc version") >= 30 {
6+
7+
let rustc_minor_ver = rustc_minor_version().expect("Failed to get rustc version");
8+
9+
// Rust >= 1.19 supports unions:
10+
if rustc_minor_ver >= 19 {
11+
println!("cargo:rustc-cfg=libc_union");
12+
}
13+
14+
// Rust >= 1.24 supports const mem::size_of:
15+
if rustc_minor_ver >= 24 {
16+
println!("cargo:rustc-cfg=libc_const_size_of");
17+
}
18+
19+
// If `core::ffi::c_void` exists, libc can just re-export it. Otherwise, it
20+
// must define an incompatible type to retain backwards-compatibility.
21+
if rustc_minor_ver >= 30 {
1122
println!("cargo:rustc-cfg=core_cvoid");
1223
}
1324
}

ci/run-docker.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ run() {
2828
--volume "$(pwd)":/checkout:ro \
2929
--volume "$(pwd)"/target:/checkout/target \
3030
--env CARGO_TARGET_DIR=/checkout/target \
31+
--env BUILD_ONLY="$BUILD_ONLY" \
3132
--workdir /checkout \
3233
libc \
3334
ci/run.sh "${1}"

ci/run.sh

Lines changed: 33 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -80,13 +80,14 @@ if [ "$QEMU" != "" ]; then
8080
exec grep "^PASSED .* tests" "${CARGO_TARGET_DIR}/out.log"
8181
fi
8282

83-
build_types="+nightly +beta +stable +1.13.0"
83+
build_types="+nightly +beta +stable +1.13.0 +1.19.0 +1.24.0 +1.30.0"
8484
for build_type in $build_types;
8585
do
86-
8786
opt=
8887
if [ "$TARGET" = "x86_64-unknown-linux-gnux32" ]; then
89-
# FIXME: x86_64-unknown-linux-gnux32 fail to compile without --release
88+
# FIXME: x86_64-unknown-linux-gnux32 fail to compile without
89+
# --release
90+
#
9091
# See https://github.com/rust-lang/rust/issues/45417
9192
opt="--release"
9293

@@ -96,21 +97,36 @@ do
9697
fi
9798
fi
9899

99-
# Building with --no-default-features is currently broken on rumprun because we
100-
# need cfg(target_vendor), which is currently unstable.
101-
if [ "$TARGET" != "x86_64-rumprun-netbsd" ]; then
102-
cargo "$build_type" test $opt --no-default-features --manifest-path libc-test/Cargo.toml --target "${TARGET}"
103-
fi
100+
if [ "$BUILD_ONLY" = "1" ]; then
101+
cargo "$build_type" build $opt --target "${TARGET}"
102+
else
103+
# Building with --no-default-features is currently broken on rumprun
104+
# because we need cfg(target_vendor), which is currently unstable.
105+
if [ "$TARGET" != "x86_64-rumprun-netbsd" ]; then
106+
cargo "$build_type" test $opt \
107+
--no-default-features \
108+
--manifest-path libc-test/Cargo.toml \
109+
--target "${TARGET}"
110+
fi
104111

105-
# Test the #[repr(align(x))] feature; requires Rust >= 1.25.0
106-
if [ "$build_type" != "+1.13.0" ]; then
107-
cargo "$build_type" test $opt --features align --manifest-path libc-test/Cargo.toml --target "${TARGET}"
108-
fi
112+
# Test the #[repr(align(x))] feature; requires Rust >= 1.25.0
113+
if [ "$build_type" != "+1.13.0" ]; then
114+
cargo "$build_type" test $opt \
115+
--features align \
116+
--manifest-path libc-test/Cargo.toml \
117+
--target "${TARGET}"
118+
fi
109119

110-
# Test the `extra_traits` feature; requires Rust >= 1.25.0
111-
if [ "$build_type" != "+1.13.0" ]; then
112-
cargo "$build_type" test $opt --features extra_traits --manifest-path libc-test/Cargo.toml --target "${TARGET}"
113-
fi
120+
# Test the `extra_traits` feature; requires Rust >= 1.25.0
121+
if [ "$build_type" != "+1.13.0" ]; then
122+
cargo "$build_type" test $opt \
123+
--features extra_traits \
124+
--manifest-path libc-test/Cargo.toml \
125+
--target "${TARGET}"
126+
fi
114127

115-
exec cargo "$build_type" test $opt --manifest-path libc-test/Cargo.toml --target "${TARGET}"
128+
cargo "$build_type" test $opt \
129+
--manifest-path libc-test/Cargo.toml \
130+
--target "${TARGET}"
131+
fi
116132
done

src/unix/bsd/apple/mod.rs

Lines changed: 40 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -513,12 +513,6 @@ s! {
513513
}
514514

515515
s_no_extra_traits!{
516-
pub union semun {
517-
pub val: ::c_int,
518-
pub buf: *mut semid_ds,
519-
pub array: *mut ::c_ushort,
520-
}
521-
522516
pub struct proc_threadinfo {
523517
pub pth_user_time: u64,
524518
pub pth_system_time: u64,
@@ -596,28 +590,41 @@ s_no_extra_traits!{
596590
}
597591
}
598592

599-
#[cfg(feature = "extra_traits")]
600-
impl PartialEq for semun {
601-
fn eq(&self, other: &semun) -> bool {
602-
unsafe { self.val == other.val }
603-
}
604-
}
605-
#[cfg(feature = "extra_traits")]
606-
impl Eq for semun {}
607-
#[cfg(feature = "extra_traits")]
608-
impl std::fmt::Debug for semun {
609-
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
610-
f.debug_struct("semun")
611-
.field("val", unsafe { &self.val })
612-
.finish()
613-
}
614-
}
615-
#[cfg(feature = "extra_traits")]
616-
impl std::hash::Hash for semun {
617-
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
618-
unsafe { self.val.hash(state) };
593+
cfg_if! {
594+
if #[cfg(libc_union)] {
595+
s_no_extra_traits! {
596+
pub union semun {
597+
pub val: ::c_int,
598+
pub buf: *mut semid_ds,
599+
pub array: *mut ::c_ushort,
600+
}
601+
}
602+
603+
cfg_if! {
604+
if #[cfg(feature = "extra_traits")] {
605+
impl PartialEq for semun {
606+
fn eq(&self, other: &semun) -> bool {
607+
unsafe { self.val == other.val }
608+
}
609+
}
610+
impl Eq for semun {}
611+
impl std::fmt::Debug for semun {
612+
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
613+
f.debug_struct("semun")
614+
.field("val", unsafe { &self.val })
615+
.finish()
616+
}
617+
}
618+
impl std::hash::Hash for semun {
619+
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
620+
unsafe { self.val.hash(state) };
621+
}
622+
}
623+
}
624+
}
619625
}
620626
}
627+
621628
#[cfg(feature = "extra_traits")]
622629
impl PartialEq for proc_threadinfo {
623630
fn eq(&self, other: &proc_threadinfo) -> bool {
@@ -2765,11 +2772,18 @@ pub const SF_IMMUTABLE: ::c_uint = 0x00020000;
27652772
pub const SF_APPEND: ::c_uint = 0x00040000;
27662773
pub const UF_HIDDEN: ::c_uint = 0x00008000;
27672774

2775+
#[cfg(libc_const_size_of)]
27682776
fn __DARWIN_ALIGN32(p: usize) -> usize {
27692777
const __DARWIN_ALIGNBYTES32: usize = mem::size_of::<u32>() - 1;
27702778
p + __DARWIN_ALIGNBYTES32 & !__DARWIN_ALIGNBYTES32
27712779
}
27722780

2781+
#[cfg(not(libc_const_size_of))]
2782+
fn __DARWIN_ALIGN32(p: usize) -> usize {
2783+
let __DARWIN_ALIGNBYTES32: usize = mem::size_of::<u32>() - 1;
2784+
p + __DARWIN_ALIGNBYTES32 & !__DARWIN_ALIGNBYTES32
2785+
}
2786+
27732787
f! {
27742788
pub fn CMSG_NXTHDR(mhdr: *const ::msghdr,
27752789
cmsg: *const ::cmsghdr) -> *mut ::cmsghdr {

src/unix/bsd/netbsdlike/openbsdlike/openbsd/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ s! {
5151
pub mount_info: mount_info,
5252
}
5353

54+
#[cfg(libc_union)]
5455
pub union mount_info {
5556
pub ufs_args: ufs_args,
5657
pub mfs_args: mfs_args,

0 commit comments

Comments
 (0)