2019-03-30 16:47:21 +01:00
|
|
|
extern crate cbindgen;
|
|
|
|
|
|
|
|
use cbindgen::*;
|
2020-04-13 12:14:58 +03:00
|
|
|
use std::path::Path;
|
2019-03-30 16:47:21 +01:00
|
|
|
use std::process::Command;
|
2019-05-31 23:52:26 +09:00
|
|
|
use std::{env, fs, str};
|
2019-03-30 16:47:21 +01:00
|
|
|
|
2020-01-26 01:57:37 +01:00
|
|
|
fn style_str(style: Style) -> &'static str {
|
|
|
|
match style {
|
|
|
|
Style::Both => "both",
|
|
|
|
Style::Tag => "tag",
|
|
|
|
Style::Type => "type",
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-30 16:47:21 +01:00
|
|
|
fn run_cbindgen(
|
2019-05-23 16:40:06 +02:00
|
|
|
cbindgen_path: &'static str,
|
2019-03-30 16:47:21 +01:00
|
|
|
path: &Path,
|
|
|
|
output: &Path,
|
|
|
|
language: Language,
|
2019-05-31 23:56:04 +09:00
|
|
|
cpp_compat: bool,
|
2019-03-30 16:47:21 +01:00
|
|
|
style: Option<Style>,
|
|
|
|
) {
|
2019-05-23 16:40:06 +02:00
|
|
|
let program = Path::new(cbindgen_path);
|
|
|
|
let mut command = Command::new(&program);
|
2019-03-30 16:47:21 +01:00
|
|
|
match language {
|
|
|
|
Language::Cxx => {}
|
|
|
|
Language::C => {
|
|
|
|
command.arg("--lang").arg("c");
|
2019-05-31 23:56:04 +09:00
|
|
|
|
|
|
|
if cpp_compat {
|
|
|
|
command.arg("--cpp-compat");
|
|
|
|
}
|
2019-03-30 16:47:21 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if let Some(style) = style {
|
2020-01-26 01:57:37 +01:00
|
|
|
command.arg("--style").arg(style_str(style));
|
2019-03-30 16:47:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
command.arg("-o").arg(output);
|
|
|
|
|
|
|
|
if env::var("CBINDGEN_TEST_VERIFY").is_ok() {
|
|
|
|
command.arg("--verify");
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut config = path.clone().to_path_buf();
|
|
|
|
config.set_extension("toml");
|
|
|
|
if config.exists() {
|
|
|
|
command.arg("--config").arg(config);
|
|
|
|
}
|
|
|
|
|
|
|
|
command.arg(path);
|
|
|
|
|
|
|
|
println!("Running: {:?}", command);
|
|
|
|
let cbindgen_output = command.output().expect("failed to execute process");
|
|
|
|
assert!(
|
|
|
|
cbindgen_output.status.success(),
|
2019-05-31 23:52:26 +09:00
|
|
|
"cbindgen failed: {:?} with error: {}",
|
|
|
|
output,
|
|
|
|
str::from_utf8(&cbindgen_output.stderr).unwrap_or_default()
|
2019-03-30 16:47:21 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-04-04 17:51:57 +02:00
|
|
|
fn compile(
|
|
|
|
cbindgen_output: &Path,
|
|
|
|
tests_path: &Path,
|
|
|
|
tmp_dir: &Path,
|
|
|
|
language: Language,
|
|
|
|
style: Option<Style>,
|
2020-04-13 12:14:58 +03:00
|
|
|
skip_warning_as_error: bool,
|
2020-04-04 17:51:57 +02:00
|
|
|
) {
|
2019-05-31 23:53:45 +09:00
|
|
|
let cc = match language {
|
|
|
|
Language::Cxx => env::var("CXX").unwrap_or_else(|_| "g++".to_owned()),
|
|
|
|
Language::C => env::var("CC").unwrap_or_else(|_| "gcc".to_owned()),
|
|
|
|
};
|
2019-03-30 16:47:21 +01:00
|
|
|
|
2020-04-04 23:45:18 +02:00
|
|
|
let file_name = cbindgen_output
|
|
|
|
.file_name()
|
|
|
|
.expect("cbindgen output should be a file");
|
|
|
|
let mut object = tmp_dir.join(file_name);
|
2019-03-30 16:47:21 +01:00
|
|
|
object.set_extension("o");
|
|
|
|
|
|
|
|
let mut command = Command::new(cc);
|
|
|
|
command.arg("-D").arg("DEFINED");
|
2020-01-26 02:18:32 +01:00
|
|
|
command.arg("-I").arg(tests_path);
|
2020-01-23 22:38:55 -08:00
|
|
|
command.arg("-Wall");
|
2020-04-13 12:14:58 +03:00
|
|
|
if !skip_warning_as_error {
|
|
|
|
command.arg("-Werror");
|
|
|
|
}
|
2020-01-23 22:38:55 -08:00
|
|
|
// `swift_name` is not recognzied by gcc.
|
|
|
|
command.arg("-Wno-attributes");
|
2020-07-31 12:06:16 +02:00
|
|
|
// clang warns about unused const variables.
|
|
|
|
command.arg("-Wno-unused-const-variable");
|
|
|
|
// clang also warns about returning non-instantiated templates (they could
|
|
|
|
// be specialized, but they're not so it's fine).
|
|
|
|
command.arg("-Wno-return-type-c-linkage");
|
2019-05-10 17:58:52 -04:00
|
|
|
if let Language::Cxx = language {
|
|
|
|
// enum class is a c++11 extension which makes g++ on macos 10.14 error out
|
2020-01-23 22:36:04 -08:00
|
|
|
// inline variables are are a c++17 extension
|
|
|
|
command.arg("-std=c++17");
|
2020-07-31 12:06:16 +02:00
|
|
|
// Prevents warnings when compiling .c files as c++.
|
|
|
|
command.arg("-x").arg("c++");
|
2020-02-24 12:45:22 -08:00
|
|
|
if let Ok(extra_flags) = env::var("CXXFLAGS") {
|
|
|
|
command.args(extra_flags.split_whitespace());
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if let Ok(extra_flags) = env::var("CFLAGS") {
|
|
|
|
command.args(extra_flags.split_whitespace());
|
|
|
|
}
|
2019-05-10 17:58:52 -04:00
|
|
|
}
|
2019-03-30 16:47:21 +01:00
|
|
|
|
2020-01-26 01:57:37 +01:00
|
|
|
if let Some(style) = style {
|
|
|
|
command.arg("-D");
|
|
|
|
command.arg(format!(
|
|
|
|
"CBINDGEN_STYLE_{}",
|
|
|
|
style_str(style).to_uppercase()
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
2020-07-31 12:06:16 +02:00
|
|
|
command.arg("-o").arg(&object);
|
|
|
|
command.arg("-c").arg(cbindgen_output);
|
|
|
|
|
2019-03-30 16:47:21 +01:00
|
|
|
println!("Running: {:?}", command);
|
|
|
|
let out = command.output().expect("failed to compile");
|
|
|
|
assert!(out.status.success(), "Output failed to compile: {:?}", out);
|
|
|
|
|
|
|
|
if object.exists() {
|
|
|
|
fs::remove_file(object).unwrap();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-29 11:03:24 +02:00
|
|
|
const SKIP_CPP_SUFFIX: &'static str = ".skip_cpp";
|
|
|
|
const SKIP_WARNING_AS_ERROR_SUFFIX: &'static str = ".skip_warning_as_error";
|
|
|
|
|
2019-03-30 16:47:21 +01:00
|
|
|
fn run_compile_test(
|
2019-05-23 16:40:06 +02:00
|
|
|
cbindgen_path: &'static str,
|
2019-03-30 16:47:21 +01:00
|
|
|
name: &'static str,
|
|
|
|
path: &Path,
|
2020-04-04 17:51:57 +02:00
|
|
|
tmp_dir: &Path,
|
2019-03-30 16:47:21 +01:00
|
|
|
language: Language,
|
2019-05-31 23:56:04 +09:00
|
|
|
cpp_compat: bool,
|
2019-03-30 16:47:21 +01:00
|
|
|
style: Option<Style>,
|
|
|
|
) {
|
|
|
|
let crate_dir = env::var("CARGO_MANIFEST_DIR").unwrap();
|
2020-01-26 02:18:32 +01:00
|
|
|
let tests_path = Path::new(&crate_dir).join("tests");
|
2020-04-13 12:14:58 +03:00
|
|
|
let mut generated_file = tests_path.join("expectations");
|
2019-03-30 16:47:21 +01:00
|
|
|
if let Some(style) = style {
|
|
|
|
match style {
|
|
|
|
Style::Both => {
|
2020-04-13 12:14:58 +03:00
|
|
|
generated_file.push("both");
|
2019-03-30 16:47:21 +01:00
|
|
|
}
|
|
|
|
Style::Tag => {
|
2020-04-13 12:14:58 +03:00
|
|
|
generated_file.push("tag");
|
2019-03-30 16:47:21 +01:00
|
|
|
}
|
|
|
|
Style::Type => {}
|
|
|
|
}
|
|
|
|
}
|
2019-05-31 23:56:04 +09:00
|
|
|
|
|
|
|
let ext = match language {
|
|
|
|
Language::Cxx => "cpp",
|
2019-03-30 16:47:21 +01:00
|
|
|
Language::C => {
|
2019-05-31 23:56:04 +09:00
|
|
|
if cpp_compat {
|
|
|
|
"compat.c"
|
|
|
|
} else {
|
|
|
|
"c"
|
|
|
|
}
|
2019-03-30 16:47:21 +01:00
|
|
|
}
|
2019-05-31 23:56:04 +09:00
|
|
|
};
|
2020-09-29 11:03:24 +02:00
|
|
|
|
|
|
|
let skip_warning_as_error = name.rfind(SKIP_WARNING_AS_ERROR_SUFFIX).is_some();
|
|
|
|
let skip_cpp = name.rfind(SKIP_CPP_SUFFIX).is_some();
|
|
|
|
|
|
|
|
let source_file = format!("{}.{}", &name, &ext)
|
|
|
|
.replace(SKIP_CPP_SUFFIX, "")
|
|
|
|
.replace(SKIP_WARNING_AS_ERROR_SUFFIX, "");
|
|
|
|
|
2020-04-13 12:14:58 +03:00
|
|
|
generated_file.push(source_file);
|
|
|
|
|
|
|
|
run_cbindgen(
|
|
|
|
cbindgen_path,
|
|
|
|
path,
|
|
|
|
&generated_file,
|
|
|
|
language,
|
|
|
|
cpp_compat,
|
|
|
|
style,
|
|
|
|
);
|
2019-05-31 23:56:04 +09:00
|
|
|
|
2020-04-13 12:14:58 +03:00
|
|
|
compile(
|
|
|
|
&generated_file,
|
|
|
|
&tests_path,
|
|
|
|
tmp_dir,
|
|
|
|
language,
|
|
|
|
style,
|
|
|
|
skip_warning_as_error,
|
|
|
|
);
|
2019-05-31 23:56:04 +09:00
|
|
|
|
2020-09-29 11:03:24 +02:00
|
|
|
if language == Language::C && cpp_compat && !skip_cpp {
|
2020-04-13 12:14:58 +03:00
|
|
|
compile(
|
|
|
|
&generated_file,
|
|
|
|
&tests_path,
|
|
|
|
tmp_dir,
|
|
|
|
Language::Cxx,
|
|
|
|
style,
|
|
|
|
skip_warning_as_error,
|
|
|
|
);
|
2019-05-31 23:56:04 +09:00
|
|
|
}
|
2019-03-30 16:47:21 +01:00
|
|
|
}
|
|
|
|
|
2019-05-23 16:40:06 +02:00
|
|
|
fn test_file(cbindgen_path: &'static str, name: &'static str, filename: &'static str) {
|
2019-03-30 16:47:21 +01:00
|
|
|
let test = Path::new(filename);
|
2020-04-04 17:51:57 +02:00
|
|
|
let tmp_dir = tempfile::Builder::new()
|
|
|
|
.prefix("cbindgen-test-output")
|
|
|
|
.tempdir()
|
|
|
|
.expect("Creating tmp dir failed");
|
|
|
|
let tmp_dir = tmp_dir.path();
|
2019-03-30 16:47:21 +01:00
|
|
|
for style in &[Style::Type, Style::Tag, Style::Both] {
|
2019-06-01 10:16:59 +09:00
|
|
|
for cpp_compat in &[true, false] {
|
|
|
|
run_compile_test(
|
|
|
|
cbindgen_path,
|
|
|
|
name,
|
|
|
|
&test,
|
2020-04-04 17:51:57 +02:00
|
|
|
tmp_dir,
|
2019-06-01 10:16:59 +09:00
|
|
|
Language::C,
|
|
|
|
*cpp_compat,
|
|
|
|
Some(*style),
|
|
|
|
);
|
|
|
|
}
|
2019-03-30 16:47:21 +01:00
|
|
|
}
|
2020-09-29 11:03:24 +02:00
|
|
|
|
|
|
|
let skip_cpp = name.rfind(SKIP_CPP_SUFFIX).is_some();
|
|
|
|
|
|
|
|
if !skip_cpp {
|
|
|
|
run_compile_test(
|
|
|
|
cbindgen_path,
|
|
|
|
name,
|
|
|
|
&test,
|
|
|
|
tmp_dir,
|
|
|
|
Language::Cxx,
|
|
|
|
/* cpp_compat = */ false,
|
|
|
|
None,
|
|
|
|
);
|
|
|
|
}
|
2019-03-30 16:47:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
macro_rules! test_file {
|
2019-05-23 16:40:06 +02:00
|
|
|
($cbindgen_path:expr, $test_function_name:ident, $name:expr, $file:tt) => {
|
2019-03-30 16:47:21 +01:00
|
|
|
#[test]
|
|
|
|
fn $test_function_name() {
|
2019-05-23 16:40:06 +02:00
|
|
|
test_file($cbindgen_path, $name, $file);
|
2019-03-30 16:47:21 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
// This file is generated by build.rs
|
|
|
|
include!(concat!(env!("OUT_DIR"), "/tests.rs"));
|