Debian Codegraph @

tests/abi.rs

raw symbols SCIP
1 // This file was generated by gir (https://github.com/gtk-rs/gir)
2 // from gir-files (https://github.com/gtk-rs/gir-files)
3 // from gst-gir-files (https://gitlab.freedesktop.org/gstreamer/gir-files-rs.git)
4 // DO NOT EDIT
5
6 #![cfg(unix)]
7
8 use gstreamer_gl_egl_sys::*;
9 use std::env;
10 use std::error::Error;
11 use std::ffi::OsString;
12 use std::mem::{align_of, size_of};
13 use std::path::Path;
14 use std::process::{Command, Stdio};
15 use std::str;
16 use tempfile::Builder;
17
18 static PACKAGES: &[&str] = &["gstreamer-gl-egl-1.0"];
19
20 #[derive(Clone, Debug)]
21 struct Compiler {
22 pub args: Vec<String>,
23 }
24
25 impl Compiler {
26 pub fn new() -> Result<Self, Box<dyn Error>> {
27 let mut args = get_var("CC", "cc")?;
28 args.push("-Wno-deprecated-declarations".to_owned());
29 // For _Generic
30 args.push("-std=c11".to_owned());
31 // For %z support in printf when using MinGW.
32 args.push("-D__USE_MINGW_ANSI_STDIO".to_owned());
33 args.extend(get_var("CFLAGS", "")?);
34 args.extend(get_var("CPPFLAGS", "")?);
35 args.extend(pkg_config_cflags(PACKAGES)?);
36 Ok(Self { args })
37 }
38
39 pub fn compile(&self, src: &Path, out: &Path) -> Result<(), Box<dyn Error>> {
40 let mut cmd = self.to_command();
41 cmd.arg(src);
42 cmd.arg("-o");
43 cmd.arg(out);
44 let status = cmd.spawn()?.wait()?;
45 if !status.success() {
46 return Err(format!("compilation command {cmd:?} failed, {status}").into());
47 }
48 Ok(())
49 }
50
51 fn to_command(&self) -> Command {
52 let mut cmd = Command::new(&self.args[0]);
53 cmd.args(&self.args[1..]);
54 cmd
55 }
56 }
57
58 fn get_var(name: &str, default: &str) -> Result<Vec<String>, Box<dyn Error>> {
59 match env::var(name) {
60 Ok(value) => Ok(shell_words::split(&value)?),
61 Err(env::VarError::NotPresent) => Ok(shell_words::split(default)?),
62 Err(err) => Err(format!("{name} {err}").into()),
63 }
64 }
65
66 fn pkg_config_cflags(packages: &[&str]) -> Result<Vec<String>, Box<dyn Error>> {
67 if packages.is_empty() {
68 return Ok(Vec::new());
69 }
70 let pkg_config = env::var_os("PKG_CONFIG").unwrap_or_else(|| OsString::from("pkg-config"));
71 let mut cmd = Command::new(pkg_config);
72 cmd.arg("--cflags");
73 cmd.args(packages);
74 cmd.stderr(Stdio::inherit());
75 let out = cmd.output()?;
76 if !out.status.success() {
77 let (status, stdout) = (out.status, String::from_utf8_lossy(&out.stdout));
78 return Err(format!("command {cmd:?} failed, {status:?}\nstdout: {stdout}").into());
79 }
80 let stdout = str::from_utf8(&out.stdout)?;
81 Ok(shell_words::split(stdout.trim())?)
82 }
83
84 #[derive(Copy, Clone, Debug, Eq, PartialEq)]
85 struct Layout {
86 size: usize,
87 alignment: usize,
88 }
89
90 #[derive(Copy, Clone, Debug, Default, Eq, PartialEq)]
91 struct Results {
92 /// Number of successfully completed tests.
93 passed: usize,
94 /// Total number of failed tests (including those that failed to compile).
95 failed: usize,
96 }
97
98 impl Results {
99 fn record_passed(&mut self) {
100 self.passed += 1;
101 }
102 fn record_failed(&mut self) {
103 self.failed += 1;
104 }
105 fn summary(&self) -> String {
106 format!("{} passed; {} failed", self.passed, self.failed)
107 }
108 fn expect_total_success(&self) {
109 if self.failed == 0 {
110 println!("OK: {}", self.summary());
111 } else {
112 panic!("FAILED: {}", self.summary());
113 };
114 }
115 }
116
117 #[test]
118 fn cross_validate_constants_with_c() {
119 let mut c_constants: Vec<(String, String)> = Vec::new();
120
121 for l in get_c_output("constant").unwrap().lines() {
122 let (name, value) = l.split_once(';').expect("Missing ';' separator");
123 c_constants.push((name.to_owned(), value.to_owned()));
124 }
125
126 let mut results = Results::default();
127
128 for ((rust_name, rust_value), (c_name, c_value)) in
129 RUST_CONSTANTS.iter().zip(c_constants.iter())
130 {
131 if rust_name != c_name {
132 results.record_failed();
133 eprintln!("Name mismatch:\nRust: {rust_name:?}\nC: {c_name:?}");
134 continue;
135 }
136
137 if rust_value != c_value {
138 results.record_failed();
139 eprintln!(
140 "Constant value mismatch for {rust_name}\nRust: {rust_value:?}\nC: {c_value:?}",
141 );
142 continue;
143 }
144
145 results.record_passed();
146 }
147
148 results.expect_total_success();
149 }
150
151 #[test]
152 fn cross_validate_layout_with_c() {
153 let mut c_layouts = Vec::new();
154
155 for l in get_c_output("layout").unwrap().lines() {
156 let (name, value) = l.split_once(';').expect("Missing first ';' separator");
157 let (size, alignment) = value.split_once(';').expect("Missing second ';' separator");
158 let size = size.parse().expect("Failed to parse size");
159 let alignment = alignment.parse().expect("Failed to parse alignment");
160 c_layouts.push((name.to_owned(), Layout { size, alignment }));
161 }
162
163 let mut results = Results::default();
164
165 for ((rust_name, rust_layout), (c_name, c_layout)) in RUST_LAYOUTS.iter().zip(c_layouts.iter())
166 {
167 if rust_name != c_name {
168 results.record_failed();
169 eprintln!("Name mismatch:\nRust: {rust_name:?}\nC: {c_name:?}");
170 continue;
171 }
172
173 if rust_layout != c_layout {
174 results.record_failed();
175 eprintln!("Layout mismatch for {rust_name}\nRust: {rust_layout:?}\nC: {c_layout:?}",);
176 continue;
177 }
178
179 results.record_passed();
180 }
181
182 results.expect_total_success();
183 }
184
185 fn get_c_output(name: &str) -> Result<String, Box<dyn Error>> {
186 let tmpdir = Builder::new().prefix("abi").tempdir()?;
187 let exe = tmpdir.path().join(name);
188 let c_file = Path::new("tests").join(name).with_extension("c");
189
190 let cc = Compiler::new().expect("configured compiler");
191 cc.compile(&c_file, &exe)?;
192
193 let mut cmd = Command::new(exe);
194 cmd.stderr(Stdio::inherit());
195 let out = cmd.output()?;
196 if !out.status.success() {
197 let (status, stdout) = (out.status, String::from_utf8_lossy(&out.stdout));
198 return Err(format!("command {cmd:?} failed, {status:?}\nstdout: {stdout}").into());
199 }
200
201 Ok(String::from_utf8(out.stdout)?)
202 }
203
204 const RUST_LAYOUTS: &[(&str, Layout)] = &[
205 (
206 "GstGLDisplayEGL",
207 Layout {
208 size: size_of::<GstGLDisplayEGL>(),
209 alignment: align_of::<GstGLDisplayEGL>(),
210 },
211 ),
212 (
213 "GstGLDisplayEGLClass",
214 Layout {
215 size: size_of::<GstGLDisplayEGLClass>(),
216 alignment: align_of::<GstGLDisplayEGLClass>(),
217 },
218 ),
219 ];
220
221 const RUST_CONSTANTS: &[(&str, &str)] = &[("GST_GL_DISPLAY_EGL_NAME", "gst.gl.display.egl")];