| 1 |
extern crate cbindgen; |
| 2 |
|
| 3 |
use cbindgen::*; |
| 4 |
use std::collections::HashSet; |
| 5 |
use std::fs::File; |
| 6 |
use std::io::Read; |
| 7 |
use std::path::Path; |
| 8 |
use std::process::Command; |
| 9 |
use std::{env, fs, str}; |
| 10 |
|
| 11 |
// Set automatically by cargo for integration tests |
| 12 |
static CBINDGEN_PATH: &str = env!("CARGO_BIN_EXE_cbindgen"); |
| 13 |
|
| 14 |
fn style_str(style: Style) -> &'static str { |
| 15 |
match style { |
| 16 |
Style::Both => "both", |
| 17 |
Style::Tag => "tag", |
| 18 |
Style::Type => "type", |
| 19 |
} |
| 20 |
} |
| 21 |
|
| 22 |
fn run_cbindgen( |
| 23 |
path: &Path, |
| 24 |
output: Option<&Path>, |
| 25 |
language: Language, |
| 26 |
cpp_compat: bool, |
| 27 |
style: Option<Style>, |
| 28 |
generate_depfile: bool, |
| 29 |
) -> (Vec<u8>, Option<String>) { |
| 30 |
assert!( |
| 31 |
!(output.is_none() && generate_depfile), |
| 32 |
"generating a depfile requires outputting to a path" |
| 33 |
); |
| 34 |
let program = Path::new(CBINDGEN_PATH); |
| 35 |
let mut command = Command::new(program); |
| 36 |
if let Some(output) = output { |
| 37 |
command.arg("--output").arg(output); |
| 38 |
} |
| 39 |
let cbindgen_depfile = if generate_depfile { |
| 40 |
let depfile = tempfile::NamedTempFile::new().unwrap(); |
| 41 |
command.arg("--depfile").arg(depfile.path()); |
| 42 |
Some(depfile) |
| 43 |
} else { |
| 44 |
None |
| 45 |
}; |
| 46 |
|
| 47 |
match language { |
| 48 |
Language::Cxx => {} |
| 49 |
Language::C => { |
| 50 |
command.arg("--lang").arg("c"); |
| 51 |
|
| 52 |
if cpp_compat { |
| 53 |
command.arg("--cpp-compat"); |
| 54 |
} |
| 55 |
} |
| 56 |
Language::Cython => { |
| 57 |
command.arg("--lang").arg("cython"); |
| 58 |
} |
| 59 |
} |
| 60 |
|
| 61 |
if let Some(style) = style { |
| 62 |
command.arg("--style").arg(style_str(style)); |
| 63 |
} |
| 64 |
|
| 65 |
let config = path.with_extension("toml"); |
| 66 |
if config.exists() { |
| 67 |
command.arg("--config").arg(config); |
| 68 |
} |
| 69 |
|
| 70 |
command.arg(path); |
| 71 |
|
| 72 |
println!("Running: {:?}", command); |
| 73 |
let cbindgen_output = command.output().expect("failed to execute process"); |
| 74 |
|
| 75 |
assert!( |
| 76 |
cbindgen_output.status.success(), |
| 77 |
"cbindgen failed: {:?} with error: {}", |
| 78 |
output, |
| 79 |
str::from_utf8(&cbindgen_output.stderr).unwrap_or_default() |
| 80 |
); |
| 81 |
|
| 82 |
let bindings = if let Some(output_path) = output { |
| 83 |
let mut bindings = Vec::new(); |
| 84 |
// Ignore errors here, we have assertions on the expected output later. |
| 85 |
let _ = File::open(output_path).map(|mut file| { |
| 86 |
let _ = file.read_to_end(&mut bindings); |
| 87 |
}); |
| 88 |
bindings |
| 89 |
} else { |
| 90 |
cbindgen_output.stdout |
| 91 |
}; |
| 92 |
|
| 93 |
let depfile_contents = if let Some(mut depfile) = cbindgen_depfile { |
| 94 |
let mut raw = Vec::new(); |
| 95 |
depfile.read_to_end(&mut raw).unwrap(); |
| 96 |
Some( |
| 97 |
str::from_utf8(raw.as_slice()) |
| 98 |
.expect("Invalid encoding encountered in depfile") |
| 99 |
.into(), |
| 100 |
) |
| 101 |
} else { |
| 102 |
None |
| 103 |
}; |
| 104 |
(bindings, depfile_contents) |
| 105 |
} |
| 106 |
|
| 107 |
fn compile( |
| 108 |
cbindgen_output: &Path, |
| 109 |
tests_path: &Path, |
| 110 |
tmp_dir: &Path, |
| 111 |
language: Language, |
| 112 |
style: Option<Style>, |
| 113 |
skip_warning_as_error: bool, |
| 114 |
) { |
| 115 |
let cc = match language { |
| 116 |
Language::Cxx => env::var("CXX").unwrap_or_else(|_| "g++".to_owned()), |
| 117 |
Language::C => env::var("CC").unwrap_or_else(|_| "gcc".to_owned()), |
| 118 |
Language::Cython => env::var("CYTHON").unwrap_or_else(|_| "cython".to_owned()), |
| 119 |
}; |
| 120 |
|
| 121 |
let file_name = cbindgen_output |
| 122 |
.file_name() |
| 123 |
.expect("cbindgen output should be a file"); |
| 124 |
let mut object = tmp_dir.join(file_name); |
| 125 |
object.set_extension("o"); |
| 126 |
|
| 127 |
let mut command = Command::new(cc); |
| 128 |
match language { |
| 129 |
Language::Cxx | Language::C => { |
| 130 |
command.arg("-D").arg("DEFINED"); |
| 131 |
command.arg("-I").arg(tests_path); |
| 132 |
command.arg("-Wall"); |
| 133 |
if !skip_warning_as_error { |
| 134 |
command.arg("-Werror"); |
| 135 |
} |
| 136 |
// `swift_name` is not recognzied by gcc. |
| 137 |
command.arg("-Wno-attributes"); |
| 138 |
// clang warns about unused const variables. |
| 139 |
command.arg("-Wno-unused-const-variable"); |
| 140 |
// clang also warns about returning non-instantiated templates (they could |
| 141 |
// be specialized, but they're not so it's fine). |
| 142 |
command.arg("-Wno-return-type-c-linkage"); |
| 143 |
if let Language::Cxx = language { |
| 144 |
// enum class is a c++11 extension which makes g++ on macos 10.14 error out |
| 145 |
// inline variables are are a c++17 extension |
| 146 |
command.arg("-std=c++17"); |
| 147 |
// Prevents warnings when compiling .c files as c++. |
| 148 |
command.arg("-x").arg("c++"); |
| 149 |
if let Ok(extra_flags) = env::var("CXXFLAGS") { |
| 150 |
command.args(extra_flags.split_whitespace()); |
| 151 |
} |
| 152 |
} else if let Ok(extra_flags) = env::var("CFLAGS") { |
| 153 |
command.args(extra_flags.split_whitespace()); |
| 154 |
} |
| 155 |
|
| 156 |
if let Some(style) = style { |
| 157 |
command.arg("-D"); |
| 158 |
command.arg(format!( |
| 159 |
"CBINDGEN_STYLE_{}", |
| 160 |
style_str(style).to_uppercase() |
| 161 |
)); |
| 162 |
} |
| 163 |
|
| 164 |
command.arg("-o").arg(&object); |
| 165 |
command.arg("-c").arg(cbindgen_output); |
| 166 |
} |
| 167 |
Language::Cython => { |
| 168 |
command.arg("-Wextra"); |
| 169 |
if !skip_warning_as_error { |
| 170 |
command.arg("-Werror"); |
| 171 |
} |
| 172 |
command.arg("-3"); |
| 173 |
command.arg("-o").arg(&object); |
| 174 |
command.arg(cbindgen_output); |
| 175 |
} |
| 176 |
} |
| 177 |
|
| 178 |
println!("Running: {:?}", command); |
| 179 |
let out = command.output().expect("failed to compile"); |
| 180 |
assert!(out.status.success(), "Output failed to compile: {:?}", out); |
| 181 |
|
| 182 |
if object.exists() { |
| 183 |
fs::remove_file(object).unwrap(); |
| 184 |
} |
| 185 |
} |
| 186 |
|
| 187 |
const SKIP_WARNING_AS_ERROR_SUFFIX: &str = ".skip_warning_as_error"; |
| 188 |
|
| 189 |
#[allow(clippy::too_many_arguments)] |
| 190 |
fn run_compile_test( |
| 191 |
name: &'static str, |
| 192 |
path: &Path, |
| 193 |
tmp_dir: &Path, |
| 194 |
language: Language, |
| 195 |
cpp_compat: bool, |
| 196 |
style: Option<Style>, |
| 197 |
cbindgen_outputs: &mut HashSet<Vec<u8>>, |
| 198 |
) { |
| 199 |
let crate_dir = env::var("CARGO_MANIFEST_DIR").unwrap(); |
| 200 |
let tests_path = Path::new(&crate_dir).join("tests"); |
| 201 |
let mut generated_file = tests_path.join("expectations"); |
| 202 |
fs::create_dir_all(&generated_file).unwrap(); |
| 203 |
|
| 204 |
let style_ext = style |
| 205 |
.map(|style| match style { |
| 206 |
Style::Both => ".both", |
| 207 |
Style::Tag => ".tag", |
| 208 |
Style::Type => "", |
| 209 |
}) |
| 210 |
.unwrap_or_default(); |
| 211 |
let lang_ext = match language { |
| 212 |
Language::Cxx => ".cpp", |
| 213 |
Language::C if cpp_compat => ".compat.c", |
| 214 |
Language::C => ".c", |
| 215 |
// cbindgen is supposed to generate declaration files (`.pxd`), but `cython` compiler |
| 216 |
// is extension-sensitive and won't work on them, so we use implementation files (`.pyx`) |
| 217 |
// in the test suite. |
| 218 |
Language::Cython => ".pyx", |
| 219 |
}; |
| 220 |
|
| 221 |
let skip_warning_as_error = name.rfind(SKIP_WARNING_AS_ERROR_SUFFIX).is_some(); |
| 222 |
|
| 223 |
let source_file = |
| 224 |
format!("{}{}{}", name, style_ext, lang_ext).replace(SKIP_WARNING_AS_ERROR_SUFFIX, ""); |
| 225 |
|
| 226 |
generated_file.push(source_file); |
| 227 |
|
| 228 |
let (output_file, generate_depfile) = if env::var_os("CBINDGEN_TEST_VERIFY").is_some() { |
| 229 |
(None, false) |
| 230 |
} else { |
| 231 |
( |
| 232 |
Some(generated_file.as_path()), |
| 233 |
// --depfile does not work in combination with expanding yet, so we blacklist expanding tests. |
| 234 |
!(name.contains("expand") || name.contains("bitfield")), |
| 235 |
) |
| 236 |
}; |
| 237 |
|
| 238 |
let (cbindgen_output, depfile_contents) = run_cbindgen( |
| 239 |
path, |
| 240 |
output_file, |
| 241 |
language, |
| 242 |
cpp_compat, |
| 243 |
style, |
| 244 |
generate_depfile, |
| 245 |
); |
| 246 |
if generate_depfile { |
| 247 |
let depfile = depfile_contents.expect("No depfile generated"); |
| 248 |
assert!(depfile.len() > 0); |
| 249 |
let mut rules = depfile.split(':'); |
| 250 |
let target = rules.next().expect("No target found"); |
| 251 |
assert_eq!(target, generated_file.as_os_str().to_str().unwrap()); |
| 252 |
let sources = rules.next().unwrap(); |
| 253 |
// All the tests here only have one sourcefile. |
| 254 |
assert!( |
| 255 |
sources.contains(path.to_str().unwrap()), |
| 256 |
"Path: {:?}, Depfile contents: {}", |
| 257 |
path, |
| 258 |
depfile |
| 259 |
); |
| 260 |
assert_eq!(rules.count(), 0, "More than 1 rule in the depfile"); |
| 261 |
} |
| 262 |
|
| 263 |
if cbindgen_outputs.contains(&cbindgen_output) { |
| 264 |
// We already generated an identical file previously. |
| 265 |
if env::var_os("CBINDGEN_TEST_VERIFY").is_some() { |
| 266 |
assert!(!generated_file.exists()); |
| 267 |
} else if generated_file.exists() { |
| 268 |
fs::remove_file(&generated_file).unwrap(); |
| 269 |
} |
| 270 |
} else { |
| 271 |
if env::var_os("CBINDGEN_TEST_VERIFY").is_some() { |
| 272 |
let prev_cbindgen_output = fs::read(&generated_file).unwrap(); |
| 273 |
assert_eq!(cbindgen_output, prev_cbindgen_output); |
| 274 |
} else { |
| 275 |
fs::write(&generated_file, &cbindgen_output).unwrap(); |
| 276 |
} |
| 277 |
|
| 278 |
cbindgen_outputs.insert(cbindgen_output); |
| 279 |
|
| 280 |
if env::var_os("CBINDGEN_TEST_NO_COMPILE").is_some() { |
| 281 |
return; |
| 282 |
} |
| 283 |
|
| 284 |
compile( |
| 285 |
&generated_file, |
| 286 |
&tests_path, |
| 287 |
tmp_dir, |
| 288 |
language, |
| 289 |
style, |
| 290 |
skip_warning_as_error, |
| 291 |
); |
| 292 |
|
| 293 |
if language == Language::C && cpp_compat { |
| 294 |
compile( |
| 295 |
&generated_file, |
| 296 |
&tests_path, |
| 297 |
tmp_dir, |
| 298 |
Language::Cxx, |
| 299 |
style, |
| 300 |
skip_warning_as_error, |
| 301 |
); |
| 302 |
} |
| 303 |
} |
| 304 |
} |
| 305 |
|
| 306 |
fn test_file(name: &'static str, filename: &'static str) { |
| 307 |
let test = Path::new(filename); |
| 308 |
let tmp_dir = tempfile::Builder::new() |
| 309 |
.prefix("cbindgen-test-output") |
| 310 |
.tempdir() |
| 311 |
.expect("Creating tmp dir failed"); |
| 312 |
let tmp_dir = tmp_dir.path(); |
| 313 |
// Run tests in deduplication priority order. C++ compatibility tests are run first, |
| 314 |
// otherwise we would lose the C++ compiler run if they were deduplicated. |
| 315 |
let mut cbindgen_outputs = HashSet::new(); |
| 316 |
for cpp_compat in &[true, false] { |
| 317 |
for style in &[Style::Type, Style::Tag, Style::Both] { |
| 318 |
run_compile_test( |
| 319 |
name, |
| 320 |
test, |
| 321 |
tmp_dir, |
| 322 |
Language::C, |
| 323 |
*cpp_compat, |
| 324 |
Some(*style), |
| 325 |
&mut cbindgen_outputs, |
| 326 |
); |
| 327 |
} |
| 328 |
} |
| 329 |
|
| 330 |
run_compile_test( |
| 331 |
name, |
| 332 |
test, |
| 333 |
tmp_dir, |
| 334 |
Language::Cxx, |
| 335 |
/* cpp_compat = */ false, |
| 336 |
None, |
| 337 |
&mut HashSet::new(), |
| 338 |
); |
| 339 |
|
| 340 |
// `Style::Both` should be identical to `Style::Tag` for Cython. |
| 341 |
let mut cbindgen_outputs = HashSet::new(); |
| 342 |
for style in &[Style::Type, Style::Tag] { |
| 343 |
run_compile_test( |
| 344 |
name, |
| 345 |
test, |
| 346 |
tmp_dir, |
| 347 |
Language::Cython, |
| 348 |
/* cpp_compat = */ false, |
| 349 |
Some(*style), |
| 350 |
&mut cbindgen_outputs, |
| 351 |
); |
| 352 |
} |
| 353 |
} |
| 354 |
|
| 355 |
macro_rules! test_file { |
| 356 |
($test_function_name:ident, $name:expr, $file:tt) => { |
| 357 |
#[test] |
| 358 |
fn $test_function_name() { |
| 359 |
test_file($name, $file); |
| 360 |
} |
| 361 |
}; |
| 362 |
} |
| 363 |
|
| 364 |
// This file is generated by build.rs |
| 365 |
include!(concat!(env!("OUT_DIR"), "/tests.rs")); |