diff options
author | Thomas de Zeeuw <thomasdezeeuw@gmail.com> | 2021-03-13 14:13:10 +0100 |
---|---|---|
committer | Thomas de Zeeuw <thomasdezeeuw@gmail.com> | 2021-03-27 10:27:17 +0100 |
commit | c8d947050229d77b9d814f4b7f7661da91bb8cc9 (patch) | |
tree | d27e4459486f510abb3ee079fdcff37b4f0d8490 /libc-test | |
parent | a309e9124bba243d5411f2afa770a2080efab261 (diff) | |
download | rust-libc-c8d947050229d77b9d814f4b7f7661da91bb8cc9.tar.gz |
Add semver test infrastructure
This first step add the infrastructure to test if libc follows semantic
versioning.
In the build step it creates a test file which imports all functions,
constants, etc. that are expected to be public. This file is generated
from the files in the (not yet included) semver directory. These files
include the function and constants expected to be public per target
family, vendor, OS, etc.
See the do_semver function in the build file of libc-test for the
details.
Diffstat (limited to 'libc-test')
-rw-r--r-- | libc-test/Cargo.toml | 5 | ||||
-rwxr-xr-x | libc-test/build.rs | 75 | ||||
-rw-r--r-- | libc-test/test/semver.rs | 11 |
3 files changed, 90 insertions, 1 deletions
diff --git a/libc-test/Cargo.toml b/libc-test/Cargo.toml index 0fc3576206..d6c9aeef86 100644 --- a/libc-test/Cargo.toml +++ b/libc-test/Cargo.toml @@ -65,3 +65,8 @@ harness = true name = "errqueue" path = "test/errqueue.rs" harness = true + +[[test]] +name = "semver" +path = "test/semver.rs" +harness = false diff --git a/libc-test/build.rs b/libc-test/build.rs index 7333fa2e5b..3fd4c207f9 100755 --- a/libc-test/build.rs +++ b/libc-test/build.rs @@ -3,7 +3,10 @@ extern crate cc; extern crate ctest2 as ctest; -use std::env; +use std::fs::File; +use std::io::{BufRead, BufReader, BufWriter, Write}; +use std::path::{Path, PathBuf}; +use std::{env, io}; fn do_cc() { let target = env::var("TARGET").unwrap(); @@ -63,9 +66,79 @@ fn ctest_cfg() -> ctest::TestGenerator { cfg } +fn do_semver() { + let mut out = PathBuf::from(env::var("OUT_DIR").unwrap()); + out.push("semver.rs"); + let mut output = BufWriter::new(File::create(&out).unwrap()); + + let family = env::var("CARGO_CFG_TARGET_FAMILY").unwrap(); + let vendor = env::var("CARGO_CFG_TARGET_VENDOR").unwrap(); + let os = env::var("CARGO_CFG_TARGET_OS").unwrap(); + let arch = env::var("CARGO_CFG_TARGET_ARCH").unwrap(); + let target_env = env::var("CARGO_CFG_TARGET_ENV").unwrap(); + + // `libc-test/semver` dir. + let mut semver_root = + PathBuf::from(env::var("CARGO_MANIFEST_DIR").unwrap()); + semver_root.push("semver"); + + // NOTE: Windows has the same `family` as `os`, no point in including it + // twice. + if family != os { + process_semver_file(&mut output, &mut semver_root, &family); + } + process_semver_file(&mut output, &mut semver_root, &vendor); + process_semver_file(&mut output, &mut semver_root, &os); + let os_arch = format!("{}-{}", os, arch); + process_semver_file(&mut output, &mut semver_root, &os_arch); + if target_env != "" { + let os_env = format!("{}-{}", os, target_env); + process_semver_file(&mut output, &mut semver_root, &os_env); + } +} + +fn process_semver_file<W: Write, P: AsRef<Path>>( + output: &mut W, + path: &mut PathBuf, + file: P, +) { + // NOTE: `path` is reused between calls, so always remove the file again. + path.push(file); + path.set_extension("txt"); + + println!("cargo:rerun-if-changed={}", path.display()); + let input_file = match File::open(&*path) { + Ok(file) => file, + Err(ref err) if err.kind() == io::ErrorKind::NotFound => { + path.pop(); + return; + } + Err(err) => panic!("unexpected error opening file: {}", err), + }; + let input = BufReader::new(input_file); + + write!(output, "// Source: {}.\n", path.display()).unwrap(); + output.write(b"use libc::{\n").unwrap(); + for line in input.lines() { + let line = line.unwrap().into_bytes(); + match line.first() { + // Ignore comments and empty lines. + Some(b'#') | None => continue, + _ => { + output.write(b" ").unwrap(); + output.write(&line).unwrap(); + output.write(b",\n").unwrap(); + } + } + } + output.write(b"};\n\n").unwrap(); + path.pop(); +} + fn main() { do_cc(); do_ctest(); + do_semver(); } macro_rules! headers { diff --git a/libc-test/test/semver.rs b/libc-test/test/semver.rs new file mode 100644 index 0000000000..61034681cc --- /dev/null +++ b/libc-test/test/semver.rs @@ -0,0 +1,11 @@ +#![allow(unused_imports)] +#![allow(deprecated)] + +extern crate libc; + +// Generated in `build.rs`. +include!(concat!(env!("OUT_DIR"), "/semver.rs")); + +fn main() { + // The test is about the imports created in `semver.rs`. +} |