Debian Codegraph @

debian/missing-sources/gobject-sys/tests/abi.rs

raw
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 // DO NOT EDIT
4
5 #![cfg(unix)]
6
7 use gobject_sys::*;
8 use std::env;
9 use std::error::Error;
10 use std::ffi::OsString;
11 use std::mem::{align_of, size_of};
12 use std::path::Path;
13 use std::process::{Command, Stdio};
14 use std::str;
15 use tempfile::Builder;
16
17 static PACKAGES: &[&str] = &["gobject-2.0"];
18
19 #[derive(Clone, Debug)]
20 struct Compiler {
21 pub args: Vec<String>,
22 }
23
24 impl Compiler {
25 pub fn new() -> Result<Self, Box<dyn Error>> {
26 let mut args = get_var("CC", "cc")?;
27 args.push("-Wno-deprecated-declarations".to_owned());
28 // For _Generic
29 args.push("-std=c11".to_owned());
30 // For %z support in printf when using MinGW.
31 args.push("-D__USE_MINGW_ANSI_STDIO".to_owned());
32 args.extend(get_var("CFLAGS", "")?);
33 args.extend(get_var("CPPFLAGS", "")?);
34 args.extend(pkg_config_cflags(PACKAGES)?);
35 Ok(Self { args })
36 }
37
38 pub fn compile(&self, src: &Path, out: &Path) -> Result<(), Box<dyn Error>> {
39 let mut cmd = self.to_command();
40 cmd.arg(src);
41 cmd.arg("-o");
42 cmd.arg(out);
43 let status = cmd.spawn()?.wait()?;
44 if !status.success() {
45 return Err(format!("compilation command {cmd:?} failed, {status}").into());
46 }
47 Ok(())
48 }
49
50 fn to_command(&self) -> Command {
51 let mut cmd = Command::new(&self.args[0]);
52 cmd.args(&self.args[1..]);
53 cmd
54 }
55 }
56
57 fn get_var(name: &str, default: &str) -> Result<Vec<String>, Box<dyn Error>> {
58 match env::var(name) {
59 Ok(value) => Ok(shell_words::split(&value)?),
60 Err(env::VarError::NotPresent) => Ok(shell_words::split(default)?),
61 Err(err) => Err(format!("{name} {err}").into()),
62 }
63 }
64
65 fn pkg_config_cflags(packages: &[&str]) -> Result<Vec<String>, Box<dyn Error>> {
66 if packages.is_empty() {
67 return Ok(Vec::new());
68 }
69 let pkg_config = env::var_os("PKG_CONFIG").unwrap_or_else(|| OsString::from("pkg-config"));
70 let mut cmd = Command::new(pkg_config);
71 cmd.arg("--cflags");
72 cmd.args(packages);
73 cmd.stderr(Stdio::inherit());
74 let out = cmd.output()?;
75 if !out.status.success() {
76 let (status, stdout) = (out.status, String::from_utf8_lossy(&out.stdout));
77 return Err(format!("command {cmd:?} failed, {status:?}\nstdout: {stdout}").into());
78 }
79 let stdout = str::from_utf8(&out.stdout)?;
80 Ok(shell_words::split(stdout.trim())?)
81 }
82
83 #[derive(Copy, Clone, Debug, Eq, PartialEq)]
84 struct Layout {
85 size: usize,
86 alignment: usize,
87 }
88
89 #[derive(Copy, Clone, Debug, Default, Eq, PartialEq)]
90 struct Results {
91 /// Number of successfully completed tests.
92 passed: usize,
93 /// Total number of failed tests (including those that failed to compile).
94 failed: usize,
95 }
96
97 impl Results {
98 fn record_passed(&mut self) {
99 self.passed += 1;
100 }
101 fn record_failed(&mut self) {
102 self.failed += 1;
103 }
104 fn summary(&self) -> String {
105 format!("{} passed; {} failed", self.passed, self.failed)
106 }
107 fn expect_total_success(&self) {
108 if self.failed == 0 {
109 println!("OK: {}", self.summary());
110 } else {
111 panic!("FAILED: {}", self.summary());
112 };
113 }
114 }
115
116 #[test]
117 fn cross_validate_constants_with_c() {
118 let mut c_constants: Vec<(String, String)> = Vec::new();
119
120 for l in get_c_output("constant").unwrap().lines() {
121 let (name, value) = l.split_once(';').expect("Missing ';' separator");
122 c_constants.push((name.to_owned(), value.to_owned()));
123 }
124
125 let mut results = Results::default();
126
127 for ((rust_name, rust_value), (c_name, c_value)) in
128 RUST_CONSTANTS.iter().zip(c_constants.iter())
129 {
130 if rust_name != c_name {
131 results.record_failed();
132 eprintln!("Name mismatch:\nRust: {rust_name:?}\nC: {c_name:?}");
133 continue;
134 }
135
136 if rust_value != c_value {
137 results.record_failed();
138 eprintln!(
139 "Constant value mismatch for {rust_name}\nRust: {rust_value:?}\nC: {c_value:?}",
140 );
141 continue;
142 }
143
144 results.record_passed();
145 }
146
147 results.expect_total_success();
148 }
149
150 #[test]
151 fn cross_validate_layout_with_c() {
152 let mut c_layouts = Vec::new();
153
154 for l in get_c_output("layout").unwrap().lines() {
155 let (name, value) = l.split_once(';').expect("Missing first ';' separator");
156 let (size, alignment) = value.split_once(';').expect("Missing second ';' separator");
157 let size = size.parse().expect("Failed to parse size");
158 let alignment = alignment.parse().expect("Failed to parse alignment");
159 c_layouts.push((name.to_owned(), Layout { size, alignment }));
160 }
161
162 let mut results = Results::default();
163
164 for ((rust_name, rust_layout), (c_name, c_layout)) in RUST_LAYOUTS.iter().zip(c_layouts.iter())
165 {
166 if rust_name != c_name {
167 results.record_failed();
168 eprintln!("Name mismatch:\nRust: {rust_name:?}\nC: {c_name:?}");
169 continue;
170 }
171
172 if rust_layout != c_layout {
173 results.record_failed();
174 eprintln!("Layout mismatch for {rust_name}\nRust: {rust_layout:?}\nC: {c_layout:?}",);
175 continue;
176 }
177
178 results.record_passed();
179 }
180
181 results.expect_total_success();
182 }
183
184 fn get_c_output(name: &str) -> Result<String, Box<dyn Error>> {
185 let tmpdir = Builder::new().prefix("abi").tempdir()?;
186 let exe = tmpdir.path().join(name);
187 let c_file = Path::new("tests").join(name).with_extension("c");
188
189 let cc = Compiler::new().expect("configured compiler");
190 cc.compile(&c_file, &exe)?;
191
192 let mut cmd = Command::new(exe);
193 cmd.stderr(Stdio::inherit());
194 let out = cmd.output()?;
195 if !out.status.success() {
196 let (status, stdout) = (out.status, String::from_utf8_lossy(&out.stdout));
197 return Err(format!("command {cmd:?} failed, {status:?}\nstdout: {stdout}").into());
198 }
199
200 Ok(String::from_utf8(out.stdout)?)
201 }
202
203 const RUST_LAYOUTS: &[(&str, Layout)] = &[
204 (
205 "GBindingFlags",
206 Layout {
207 size: size_of::<GBindingFlags>(),
208 alignment: align_of::<GBindingFlags>(),
209 },
210 ),
211 (
212 "GClosureNotifyData",
213 Layout {
214 size: size_of::<GClosureNotifyData>(),
215 alignment: align_of::<GClosureNotifyData>(),
216 },
217 ),
218 (
219 "GConnectFlags",
220 Layout {
221 size: size_of::<GConnectFlags>(),
222 alignment: align_of::<GConnectFlags>(),
223 },
224 ),
225 (
226 "GEnumClass",
227 Layout {
228 size: size_of::<GEnumClass>(),
229 alignment: align_of::<GEnumClass>(),
230 },
231 ),
232 (
233 "GEnumValue",
234 Layout {
235 size: size_of::<GEnumValue>(),
236 alignment: align_of::<GEnumValue>(),
237 },
238 ),
239 (
240 "GFlagsClass",
241 Layout {
242 size: size_of::<GFlagsClass>(),
243 alignment: align_of::<GFlagsClass>(),
244 },
245 ),
246 (
247 "GFlagsValue",
248 Layout {
249 size: size_of::<GFlagsValue>(),
250 alignment: align_of::<GFlagsValue>(),
251 },
252 ),
253 (
254 "GIOCondition",
255 Layout {
256 size: size_of::<GIOCondition>(),
257 alignment: align_of::<GIOCondition>(),
258 },
259 ),
260 (
261 "GInitiallyUnowned",
262 Layout {
263 size: size_of::<GInitiallyUnowned>(),
264 alignment: align_of::<GInitiallyUnowned>(),
265 },
266 ),
267 (
268 "GInitiallyUnownedClass",
269 Layout {
270 size: size_of::<GInitiallyUnownedClass>(),
271 alignment: align_of::<GInitiallyUnownedClass>(),
272 },
273 ),
274 (
275 "GInterfaceInfo",
276 Layout {
277 size: size_of::<GInterfaceInfo>(),
278 alignment: align_of::<GInterfaceInfo>(),
279 },
280 ),
281 (
282 "GObject",
283 Layout {
284 size: size_of::<GObject>(),
285 alignment: align_of::<GObject>(),
286 },
287 ),
288 (
289 "GObjectClass",
290 Layout {
291 size: size_of::<GObjectClass>(),
292 alignment: align_of::<GObjectClass>(),
293 },
294 ),
295 (
296 "GObjectConstructParam",
297 Layout {
298 size: size_of::<GObjectConstructParam>(),
299 alignment: align_of::<GObjectConstructParam>(),
300 },
301 ),
302 (
303 "GParamFlags",
304 Layout {
305 size: size_of::<GParamFlags>(),
306 alignment: align_of::<GParamFlags>(),
307 },
308 ),
309 (
310 "GParamSpec",
311 Layout {
312 size: size_of::<GParamSpec>(),
313 alignment: align_of::<GParamSpec>(),
314 },
315 ),
316 (
317 "GParamSpecBoolean",
318 Layout {
319 size: size_of::<GParamSpecBoolean>(),
320 alignment: align_of::<GParamSpecBoolean>(),
321 },
322 ),
323 (
324 "GParamSpecBoxed",
325 Layout {
326 size: size_of::<GParamSpecBoxed>(),
327 alignment: align_of::<GParamSpecBoxed>(),
328 },
329 ),
330 (
331 "GParamSpecChar",
332 Layout {
333 size: size_of::<GParamSpecChar>(),
334 alignment: align_of::<GParamSpecChar>(),
335 },
336 ),
337 (
338 "GParamSpecClass",
339 Layout {
340 size: size_of::<GParamSpecClass>(),
341 alignment: align_of::<GParamSpecClass>(),
342 },
343 ),
344 (
345 "GParamSpecDouble",
346 Layout {
347 size: size_of::<GParamSpecDouble>(),
348 alignment: align_of::<GParamSpecDouble>(),
349 },
350 ),
351 (
352 "GParamSpecEnum",
353 Layout {
354 size: size_of::<GParamSpecEnum>(),
355 alignment: align_of::<GParamSpecEnum>(),
356 },
357 ),
358 (
359 "GParamSpecFlags",
360 Layout {
361 size: size_of::<GParamSpecFlags>(),
362 alignment: align_of::<GParamSpecFlags>(),
363 },
364 ),
365 (
366 "GParamSpecFloat",
367 Layout {
368 size: size_of::<GParamSpecFloat>(),
369 alignment: align_of::<GParamSpecFloat>(),
370 },
371 ),
372 (
373 "GParamSpecGType",
374 Layout {
375 size: size_of::<GParamSpecGType>(),
376 alignment: align_of::<GParamSpecGType>(),
377 },
378 ),
379 (
380 "GParamSpecInt",
381 Layout {
382 size: size_of::<GParamSpecInt>(),
383 alignment: align_of::<GParamSpecInt>(),
384 },
385 ),
386 (
387 "GParamSpecInt64",
388 Layout {
389 size: size_of::<GParamSpecInt64>(),
390 alignment: align_of::<GParamSpecInt64>(),
391 },
392 ),
393 (
394 "GParamSpecLong",
395 Layout {
396 size: size_of::<GParamSpecLong>(),
397 alignment: align_of::<GParamSpecLong>(),
398 },
399 ),
400 (
401 "GParamSpecObject",
402 Layout {
403 size: size_of::<GParamSpecObject>(),
404 alignment: align_of::<GParamSpecObject>(),
405 },
406 ),
407 (
408 "GParamSpecOverride",
409 Layout {
410 size: size_of::<GParamSpecOverride>(),
411 alignment: align_of::<GParamSpecOverride>(),
412 },
413 ),
414 (
415 "GParamSpecParam",
416 Layout {
417 size: size_of::<GParamSpecParam>(),
418 alignment: align_of::<GParamSpecParam>(),
419 },
420 ),
421 (
422 "GParamSpecPointer",
423 Layout {
424 size: size_of::<GParamSpecPointer>(),
425 alignment: align_of::<GParamSpecPointer>(),
426 },
427 ),
428 (
429 "GParamSpecTypeInfo",
430 Layout {
431 size: size_of::<GParamSpecTypeInfo>(),
432 alignment: align_of::<GParamSpecTypeInfo>(),
433 },
434 ),
435 (
436 "GParamSpecUChar",
437 Layout {
438 size: size_of::<GParamSpecUChar>(),
439 alignment: align_of::<GParamSpecUChar>(),
440 },
441 ),
442 (
443 "GParamSpecUInt",
444 Layout {
445 size: size_of::<GParamSpecUInt>(),
446 alignment: align_of::<GParamSpecUInt>(),
447 },
448 ),
449 (
450 "GParamSpecUInt64",
451 Layout {
452 size: size_of::<GParamSpecUInt64>(),
453 alignment: align_of::<GParamSpecUInt64>(),
454 },
455 ),
456 (
457 "GParamSpecULong",
458 Layout {
459 size: size_of::<GParamSpecULong>(),
460 alignment: align_of::<GParamSpecULong>(),
461 },
462 ),
463 (
464 "GParamSpecUnichar",
465 Layout {
466 size: size_of::<GParamSpecUnichar>(),
467 alignment: align_of::<GParamSpecUnichar>(),
468 },
469 ),
470 (
471 "GParamSpecValueArray",
472 Layout {
473 size: size_of::<GParamSpecValueArray>(),
474 alignment: align_of::<GParamSpecValueArray>(),
475 },
476 ),
477 (
478 "GParamSpecVariant",
479 Layout {
480 size: size_of::<GParamSpecVariant>(),
481 alignment: align_of::<GParamSpecVariant>(),
482 },
483 ),
484 (
485 "GParameter",
486 Layout {
487 size: size_of::<GParameter>(),
488 alignment: align_of::<GParameter>(),
489 },
490 ),
491 (
492 "GSignalCMarshaller",
493 Layout {
494 size: size_of::<GSignalCMarshaller>(),
495 alignment: align_of::<GSignalCMarshaller>(),
496 },
497 ),
498 (
499 "GSignalFlags",
500 Layout {
501 size: size_of::<GSignalFlags>(),
502 alignment: align_of::<GSignalFlags>(),
503 },
504 ),
505 (
506 "GSignalInvocationHint",
507 Layout {
508 size: size_of::<GSignalInvocationHint>(),
509 alignment: align_of::<GSignalInvocationHint>(),
510 },
511 ),
512 (
513 "GSignalMatchType",
514 Layout {
515 size: size_of::<GSignalMatchType>(),
516 alignment: align_of::<GSignalMatchType>(),
517 },
518 ),
519 (
520 "GSignalQuery",
521 Layout {
522 size: size_of::<GSignalQuery>(),
523 alignment: align_of::<GSignalQuery>(),
524 },
525 ),
526 (
527 "GTypeCValue",
528 Layout {
529 size: size_of::<GTypeCValue>(),
530 alignment: align_of::<GTypeCValue>(),
531 },
532 ),
533 (
534 "GTypeClass",
535 Layout {
536 size: size_of::<GTypeClass>(),
537 alignment: align_of::<GTypeClass>(),
538 },
539 ),
540 (
541 "GTypeDebugFlags",
542 Layout {
543 size: size_of::<GTypeDebugFlags>(),
544 alignment: align_of::<GTypeDebugFlags>(),
545 },
546 ),
547 (
548 "GTypeFlags",
549 Layout {
550 size: size_of::<GTypeFlags>(),
551 alignment: align_of::<GTypeFlags>(),
552 },
553 ),
554 (
555 "GTypeFundamentalFlags",
556 Layout {
557 size: size_of::<GTypeFundamentalFlags>(),
558 alignment: align_of::<GTypeFundamentalFlags>(),
559 },
560 ),
561 (
562 "GTypeFundamentalInfo",
563 Layout {
564 size: size_of::<GTypeFundamentalInfo>(),
565 alignment: align_of::<GTypeFundamentalInfo>(),
566 },
567 ),
568 (
569 "GTypeInfo",
570 Layout {
571 size: size_of::<GTypeInfo>(),
572 alignment: align_of::<GTypeInfo>(),
573 },
574 ),
575 (
576 "GTypeInstance",
577 Layout {
578 size: size_of::<GTypeInstance>(),
579 alignment: align_of::<GTypeInstance>(),
580 },
581 ),
582 (
583 "GTypeInterface",
584 Layout {
585 size: size_of::<GTypeInterface>(),
586 alignment: align_of::<GTypeInterface>(),
587 },
588 ),
589 (
590 "GTypeModule",
591 Layout {
592 size: size_of::<GTypeModule>(),
593 alignment: align_of::<GTypeModule>(),
594 },
595 ),
596 (
597 "GTypeModuleClass",
598 Layout {
599 size: size_of::<GTypeModuleClass>(),
600 alignment: align_of::<GTypeModuleClass>(),
601 },
602 ),
603 (
604 "GTypePluginClass",
605 Layout {
606 size: size_of::<GTypePluginClass>(),
607 alignment: align_of::<GTypePluginClass>(),
608 },
609 ),
610 (
611 "GTypeQuery",
612 Layout {
613 size: size_of::<GTypeQuery>(),
614 alignment: align_of::<GTypeQuery>(),
615 },
616 ),
617 (
618 "GTypeValueTable",
619 Layout {
620 size: size_of::<GTypeValueTable>(),
621 alignment: align_of::<GTypeValueTable>(),
622 },
623 ),
624 (
625 "GValue",
626 Layout {
627 size: size_of::<GValue>(),
628 alignment: align_of::<GValue>(),
629 },
630 ),
631 (
632 "GValueArray",
633 Layout {
634 size: size_of::<GValueArray>(),
635 alignment: align_of::<GValueArray>(),
636 },
637 ),
638 (
639 "GWeakRef",
640 Layout {
641 size: size_of::<GWeakRef>(),
642 alignment: align_of::<GWeakRef>(),
643 },
644 ),
645 ];
646
647 const RUST_CONSTANTS: &[(&str, &str)] = &[
648 ("(guint) G_BINDING_BIDIRECTIONAL", "1"),
649 ("(guint) G_BINDING_DEFAULT", "0"),
650 ("(guint) G_BINDING_INVERT_BOOLEAN", "4"),
651 ("(guint) G_BINDING_SYNC_CREATE", "2"),
652 ("(guint) G_CONNECT_AFTER", "1"),
653 ("(guint) G_CONNECT_DEFAULT", "0"),
654 ("(guint) G_CONNECT_SWAPPED", "2"),
655 ("(guint) G_IO_ERR", "8"),
656 ("(guint) G_IO_HUP", "16"),
657 ("(guint) G_IO_IN", "1"),
658 ("(guint) G_IO_NVAL", "32"),
659 ("(guint) G_IO_OUT", "4"),
660 ("(guint) G_IO_PRI", "2"),
661 ("(guint) G_PARAM_CONSTRUCT", "4"),
662 ("(guint) G_PARAM_CONSTRUCT_ONLY", "8"),
663 ("(guint) G_PARAM_DEPRECATED", "2147483648"),
664 ("(guint) G_PARAM_EXPLICIT_NOTIFY", "1073741824"),
665 ("(guint) G_PARAM_LAX_VALIDATION", "16"),
666 ("G_PARAM_MASK", "255"),
667 ("(guint) G_PARAM_PRIVATE", "32"),
668 ("(guint) G_PARAM_READABLE", "1"),
669 ("(guint) G_PARAM_READWRITE", "3"),
670 ("(guint) G_PARAM_STATIC_BLURB", "128"),
671 ("(guint) G_PARAM_STATIC_NAME", "32"),
672 ("(guint) G_PARAM_STATIC_NICK", "64"),
673 ("G_PARAM_STATIC_STRINGS", "224"),
674 ("G_PARAM_USER_SHIFT", "8"),
675 ("(guint) G_PARAM_WRITABLE", "2"),
676 ("(guint) G_SIGNAL_ACCUMULATOR_FIRST_RUN", "131072"),
677 ("(guint) G_SIGNAL_ACTION", "32"),
678 ("(guint) G_SIGNAL_DEPRECATED", "256"),
679 ("(guint) G_SIGNAL_DETAILED", "16"),
680 ("G_SIGNAL_FLAGS_MASK", "511"),
681 ("(guint) G_SIGNAL_MATCH_CLOSURE", "4"),
682 ("(guint) G_SIGNAL_MATCH_DATA", "16"),
683 ("(guint) G_SIGNAL_MATCH_DETAIL", "2"),
684 ("(guint) G_SIGNAL_MATCH_FUNC", "8"),
685 ("(guint) G_SIGNAL_MATCH_ID", "1"),
686 ("G_SIGNAL_MATCH_MASK", "63"),
687 ("(guint) G_SIGNAL_MATCH_UNBLOCKED", "32"),
688 ("(guint) G_SIGNAL_MUST_COLLECT", "128"),
689 ("(guint) G_SIGNAL_NO_HOOKS", "64"),
690 ("(guint) G_SIGNAL_NO_RECURSE", "8"),
691 ("(guint) G_SIGNAL_RUN_CLEANUP", "4"),
692 ("(guint) G_SIGNAL_RUN_FIRST", "1"),
693 ("(guint) G_SIGNAL_RUN_LAST", "2"),
694 ("(guint) G_TYPE_DEBUG_INSTANCE_COUNT", "4"),
695 ("(guint) G_TYPE_DEBUG_MASK", "7"),
696 ("(guint) G_TYPE_DEBUG_NONE", "0"),
697 ("(guint) G_TYPE_DEBUG_OBJECTS", "1"),
698 ("(guint) G_TYPE_DEBUG_SIGNALS", "2"),
699 ("(guint) G_TYPE_FLAG_ABSTRACT", "16"),
700 ("(guint) G_TYPE_FLAG_CLASSED", "1"),
701 ("(guint) G_TYPE_FLAG_DEEP_DERIVABLE", "8"),
702 ("(guint) G_TYPE_FLAG_DEPRECATED", "128"),
703 ("(guint) G_TYPE_FLAG_DERIVABLE", "4"),
704 ("(guint) G_TYPE_FLAG_FINAL", "64"),
705 ("(guint) G_TYPE_FLAG_INSTANTIATABLE", "2"),
706 ("(guint) G_TYPE_FLAG_NONE", "0"),
707 ("G_TYPE_FLAG_RESERVED_ID_BIT", "1"),
708 ("(guint) G_TYPE_FLAG_VALUE_ABSTRACT", "32"),
709 ("G_TYPE_FUNDAMENTAL_MAX", "1020"),
710 ("G_TYPE_FUNDAMENTAL_SHIFT", "2"),
711 ("G_TYPE_RESERVED_BSE_FIRST", "32"),
712 ("G_TYPE_RESERVED_BSE_LAST", "48"),
713 ("G_TYPE_RESERVED_GLIB_FIRST", "22"),
714 ("G_TYPE_RESERVED_GLIB_LAST", "31"),
715 ("G_TYPE_RESERVED_USER_FIRST", "49"),
716 ("G_VALUE_COLLECT_FORMAT_MAX_LENGTH", "8"),
717 ("G_VALUE_INTERNED_STRING", "268435456"),
718 ("G_VALUE_NOCOPY_CONTENTS", "134217728"),
719 ];