Skip to content

Commit

Permalink
Add more fcntl and seal constants for Android/Linux
Browse files Browse the repository at this point in the history
We now create an additional binary `linux_fcntl` for testing this
since there are header conflicts when including all necessary headers.
This binary is run on all platforms even though it's empty on all non-
Android/non-Linux platforms.

Testing has been switched from a custom binary to using a runner-less
test (or pair of tests). This means that for local development a simple
`cd libc-test && cargo test` will run all the tests. CI has also been
updated here to reflect that.
  • Loading branch information
Susurrus committed Aug 11, 2017
1 parent 09d9209 commit a4b6e2c
Show file tree
Hide file tree
Showing 11 changed files with 115 additions and 76 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ With that in mind, the steps for adding a new API are:
We have two automated tests running on [Travis](https://travis-ci.org/rust-lang/libc):

1. [`libc-test`](https://github.com/alexcrichton/ctest)
- `cd libc-test && cargo run`
- `cd libc-test && cargo test`
- Use the `skip_*()` functions in `build.rs` if you really need a workaround.
2. Style checker
- `rustc ci/style.rs && ./style src`
Expand Down
2 changes: 1 addition & 1 deletion appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ build: false

test_script:
- cargo test --target %TARGET%
- cargo run --manifest-path libc-test/Cargo.toml --target %TARGET%
- cargo test --manifest-path libc-test/Cargo.toml --target %TARGET%

cache:
- target
Expand Down
66 changes: 45 additions & 21 deletions ci/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,10 @@ if [ "$QEMU" != "" ]; then
# be compiled inside QEMU. Do this here because compiling syntex_syntax in
# QEMU would time out basically everywhere.
if [ "$CAN_CROSS" = "1" ]; then
cargo build --manifest-path libc-test/Cargo.toml --target $TARGET
cp $CARGO_TARGET_DIR/$TARGET/debug/libc-test $tmpdir/mount/
echo 'exec $1/libc-test' > $tmpdir/mount/run.sh
cargo build --manifest-path libc-test/Cargo.toml --target $TARGET --tests
cp $CARGO_TARGET_DIR/$TARGET/debug/main-* $tmpdir/mount/main
cp $CARGO_TARGET_DIR/$TARGET/debug/linux_fcntl-* $tmpdir/mount/linux_fcntl
echo '$1/main && $1/linux_fcntl' > $tmpdir/mount/run.sh
else
rm -rf $tmpdir/generated
mkdir -p $tmpdir/generated
Expand Down Expand Up @@ -95,12 +96,14 @@ fi

case "$TARGET" in
*-apple-ios)
cargo rustc --manifest-path libc-test/Cargo.toml --target $TARGET -- \
-C link-args=-mios-simulator-version-min=7.0
cargo rustc --manifest-path libc-test/Cargo.toml --target $TARGET \
--test main -- -C link-args=-mios-simulator-version-min=7.0
cargo rustc --manifest-path libc-test/Cargo.toml --target $TARGET \
--test linux-fcntl -- -C link-args=-mios-simulator-version-min=7.0
;;

*)
cargo build --manifest-path libc-test/Cargo.toml --target $TARGET
cargo build --manifest-path libc-test/Cargo.toml --target $TARGET --tests
;;
esac

Expand All @@ -120,68 +123,89 @@ case "$TARGET" in
fi
emulator @$arch -no-window $accel &
adb wait-for-device
adb push $CARGO_TARGET_DIR/$TARGET/debug/libc-test /data/local/tmp/libc-test
adb shell /data/local/tmp/libc-test 2>&1 | tee /tmp/out
adb push $CARGO_TARGET_DIR/$TARGET/debug/main-* /data/local/tmp/main
adb shell /data/local/tmp/main 2>&1 | tee /tmp/out
grep "^PASSED .* tests" /tmp/out
adb push $CARGO_TARGET_DIR/$TARGET/debug/linux_fcntl-* /data/local/tmp/linux_fcntl
adb shell /data/local/tmp/linux_fcntl 2>&1 | tee /tmp/out
grep "^PASSED .* tests" /tmp/out
;;

i386-apple-ios)
rustc -O ./ci/ios/deploy_and_run_on_ios_simulator.rs
./deploy_and_run_on_ios_simulator $CARGO_TARGET_DIR/$TARGET/debug/libc-test
./deploy_and_run_on_ios_simulator $CARGO_TARGET_DIR/$TARGET/debug/main-*
./deploy_and_run_on_ios_simulator $CARGO_TARGET_DIR/$TARGET/debug/linux_fcntl-*
;;

x86_64-apple-ios)
rustc -O ./ci/ios/deploy_and_run_on_ios_simulator.rs
./deploy_and_run_on_ios_simulator $CARGO_TARGET_DIR/$TARGET/debug/libc-test
./deploy_and_run_on_ios_simulator $CARGO_TARGET_DIR/$TARGET/debug/main-*
./deploy_and_run_on_ios_simulator $CARGO_TARGET_DIR/$TARGET/debug/linux_fcntl-*
;;

arm-unknown-linux-gnueabihf)
qemu-arm -L /usr/arm-linux-gnueabihf $CARGO_TARGET_DIR/$TARGET/debug/libc-test
qemu-arm -L /usr/arm-linux-gnueabihf $CARGO_TARGET_DIR/$TARGET/debug/main-*
qemu-arm -L /usr/arm-linux-gnueabihf $CARGO_TARGET_DIR/$TARGET/debug/linux_fcntl-*
;;

mips-unknown-linux-gnu)
qemu-mips -L /usr/mips-linux-gnu $CARGO_TARGET_DIR/$TARGET/debug/libc-test
qemu-mips -L /usr/mips-linux-gnu $CARGO_TARGET_DIR/$TARGET/debug/main-*
qemu-mips -L /usr/mips-linux-gnu $CARGO_TARGET_DIR/$TARGET/debug/linux_fcntl-*
;;

mips64-unknown-linux-gnuabi64)
qemu-mips64 -L /usr/mips64-linux-gnuabi64 $CARGO_TARGET_DIR/$TARGET/debug/libc-test
qemu-mips64 -L /usr/mips64-linux-gnuabi64 $CARGO_TARGET_DIR/$TARGET/debug/main-*
qemu-mips64 -L /usr/mips64-linux-gnuabi64 $CARGO_TARGET_DIR/$TARGET/debug/linux_fcntl-*
;;

mips-unknown-linux-musl)
qemu-mips -L /toolchain/staging_dir/toolchain-mips_34kc_gcc-5.3.0_musl-1.1.15 \
$CARGO_TARGET_DIR/$TARGET/debug/libc-test
$CARGO_TARGET_DIR/$TARGET/debug/main-*
qemu-mips -L /toolchain/staging_dir/toolchain-mips_34kc_gcc-5.3.0_musl-1.1.15 \
$CARGO_TARGET_DIR/$TARGET/debug/linux_fcntl-*
;;

mipsel-unknown-linux-musl)
qemu-mipsel -L /toolchain $CARGO_TARGET_DIR/$TARGET/debug/libc-test
qemu-mipsel -L /toolchain $CARGO_TARGET_DIR/$TARGET/debug/main-*
qemu-mipsel -L /toolchain $CARGO_TARGET_DIR/$TARGET/debug/linux_fcntl-*
;;

powerpc-unknown-linux-gnu)
qemu-ppc -L /usr/powerpc-linux-gnu $CARGO_TARGET_DIR/$TARGET/debug/libc-test
qemu-ppc -L /usr/powerpc-linux-gnu $CARGO_TARGET_DIR/$TARGET/debug/main-*
qemu-ppc -L /usr/powerpc-linux-gnu $CARGO_TARGET_DIR/$TARGET/debug/linux_fcntl-*
;;

powerpc64-unknown-linux-gnu)
qemu-ppc64 -L /usr/powerpc64-linux-gnu $CARGO_TARGET_DIR/$TARGET/debug/libc-test
qemu-ppc64 -L /usr/powerpc64-linux-gnu $CARGO_TARGET_DIR/$TARGET/debug/main-*
qemu-ppc64 -L /usr/powerpc64-linux-gnu $CARGO_TARGET_DIR/$TARGET/debug/linux_fcntl-*
;;

aarch64-unknown-linux-gnu)
qemu-aarch64 -L /usr/aarch64-linux-gnu/ $CARGO_TARGET_DIR/$TARGET/debug/libc-test
qemu-aarch64 -L /usr/aarch64-linux-gnu/ $CARGO_TARGET_DIR/$TARGET/debug/main-*
qemu-aarch64 -L /usr/aarch64-linux-gnu/ $CARGO_TARGET_DIR/$TARGET/debug/linux_fcntl-*
;;

s390x-unknown-linux-gnu)
# TODO: in theory we should execute this, but qemu segfaults immediately :(
# qemu-s390x -L /usr/s390x-linux-gnu/ $CARGO_TARGET_DIR/$TARGET/debug/libc-test
# qemu-s390x -L /usr/s390x-linux-gnu/ $CARGO_TARGET_DIR/$TARGET/debug/main-*
# qemu-s390x -L /usr/s390x-linux-gnu/ $CARGO_TARGET_DIR/$TARGET/debug/linux_fcntl-*
;;

*-rumprun-netbsd)
rumprun-bake hw_virtio /tmp/libc-test.img $CARGO_TARGET_DIR/$TARGET/debug/libc-test
rumprun-bake hw_virtio /tmp/libc-test.img $CARGO_TARGET_DIR/$TARGET/debug/main-*
qemu-system-x86_64 -nographic -vga none -m 64 \
-kernel /tmp/libc-test.img 2>&1 | tee /tmp/out &
sleep 5
grep "^PASSED .* tests" /tmp/out
rumprun-bake hw_virtio /tmp/libc-test.img $CARGO_TARGET_DIR/$TARGET/debug/linux_fcntl-*
qemu-system-x86_64 -nographic -vga none -m 64 \
-kernel /tmp/libc-test.img 2>&1 | tee /tmp/out &
sleep 5
grep "^PASSED .* tests" /tmp/out
;;

*)
$CARGO_TARGET_DIR/$TARGET/debug/libc-test
$CARGO_TARGET_DIR/$TARGET/debug/main-*
$CARGO_TARGET_DIR/$TARGET/debug/linux_fcntl-*
;;
esac
10 changes: 10 additions & 0 deletions libc-test/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,13 @@ libc = { path = ".." }

[build-dependencies]
ctest = "0.1"

[[test]]
name = "main"
path = "test/main.rs"
harness = false

[[test]]
name = "linux-fcntl"
path = "test/linux_fcntl.rs"
harness = false
35 changes: 33 additions & 2 deletions libc-test/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -465,6 +465,11 @@ fn main() {
// it's been fixed in CI.
"MADV_SOFT_OFFLINE" if mips && linux => true,

// These constants are tested in a separate test program generated below because there
// are header conflicts if we try to include the headers that define them here.
"F_CANCELLK" | "F_ADD_SEALS" | "F_GET_SEALS" => true,
"F_SEAL_SEAL" | "F_SEAL_SHRINK" | "F_SEAL_GROW" | "F_SEAL_WRITE" => true,

_ => false,
}
});
Expand Down Expand Up @@ -637,8 +642,34 @@ fn main() {
});

if env::var("SKIP_COMPILE").is_ok() {
cfg.generate_files("../src/lib.rs", "all.rs");
cfg.generate_files("../src/lib.rs", "main.rs");
} else {
cfg.generate("../src/lib.rs", "main.rs");
}

// On Linux or Android also generate another script for testing linux/fcntl declarations.
// These cannot be tested normally because including both `linux/fcntl.h` and `fcntl.h`
// fails on a lot of platforms.
let mut cfg = ctest::TestGenerator::new();
cfg.skip_type(|_| true)
.skip_struct(|_| true)
.skip_fn(|_| true);
if android || linux {
// musl defines these directly in `fcntl.h`
if musl {
cfg.header("fcntl.h");
} else {
cfg.header("linux/fcntl.h");
}
cfg.skip_const(move |name| {
match name {
"F_CANCELLK" | "F_ADD_SEALS" | "F_GET_SEALS" => false,
"F_SEAL_SEAL" | "F_SEAL_SHRINK" | "F_SEAL_GROW" | "F_SEAL_WRITE" => false,
_ => true,
}
});
} else {
cfg.generate("../src/lib.rs", "all.rs");
cfg.skip_const(|_| true);
}
cfg.generate("../src/lib.rs", "linux_fcntl.rs");
}
14 changes: 10 additions & 4 deletions libc-test/run-generated-Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,15 @@ build = "build-generated.rs"
[dependencies]
libc = { path = ".." }

[[bin]]
name = "libc-test"
path = "src/main-generated.rs"

[build-dependencies]
gcc = "0.3"

[[test]]
name = "main"
path = "test/main.rs"
harness = false

[[test]]
name = "linux-fcntl"
path = "test/linux_fcntl.rs"
harness = false
9 changes: 0 additions & 9 deletions libc-test/src/main-generated.rs

This file was deleted.

6 changes: 6 additions & 0 deletions libc-test/test/linux_fcntl.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#![allow(bad_style, improper_ctypes)]
extern crate libc;

use libc::*;

include!(concat!(env!("OUT_DIR"), "/linux_fcntl.rs"));
2 changes: 1 addition & 1 deletion libc-test/src/main.rs → libc-test/test/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ extern crate libc;

use libc::*;

include!(concat!(env!("OUT_DIR"), "/all.rs"));
include!(concat!(env!("OUT_DIR"), "/main.rs"));
37 changes: 0 additions & 37 deletions src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,40 +69,3 @@ macro_rules! f {
macro_rules! __item {
($i:item) => ($i)
}

#[cfg(test)]
mod tests {
cfg_if! {
if #[cfg(test)] {
use std::option::Option as Option2;