#![cfg(unix)]
use gegl_sys::*;
use std::env;
use std::error::Error;
use std::ffi::OsString;
use std::mem::{align_of, size_of};
use std::path::Path;
use std::process::{Command, Stdio};
use std::str;
use tempfile::Builder;
static PACKAGES: &[&str] = &["gegl-0.4"];
#[derive(Clone, Debug)]
struct Compiler {
pub args: Vec<String>,
}
impl Compiler {
pub fn new() -> Result<Self, Box<dyn Error>> {
let mut args = get_var("CC", "cc")?;
args.push("-Wno-deprecated-declarations".to_owned());
args.push("-std=c11".to_owned());
args.push("-D__USE_MINGW_ANSI_STDIO".to_owned());
args.extend(get_var("CFLAGS", "")?);
args.extend(get_var("CPPFLAGS", "")?);
args.extend(pkg_config_cflags(PACKAGES)?);
Ok(Self { args })
}
pub fn compile(&self, src: &Path, out: &Path) -> Result<(), Box<dyn Error>> {
let mut cmd = self.to_command();
cmd.arg(src);
cmd.arg("-o");
cmd.arg(out);
let status = cmd.spawn()?.wait()?;
if !status.success() {
return Err(format!("compilation command {cmd:?} failed, {status}").into());
}
Ok(())
}
fn to_command(&self) -> Command {
let mut cmd = Command::new(&self.args[0]);
cmd.args(&self.args[1..]);
cmd
}
}
fn get_var(name: &str, default: &str) -> Result<Vec<String>, Box<dyn Error>> {
match env::var(name) {
Ok(value) => Ok(shell_words::split(&value)?),
Err(env::VarError::NotPresent) => Ok(shell_words::split(default)?),
Err(err) => Err(format!("{name} {err}").into()),
}
}
fn pkg_config_cflags(packages: &[&str]) -> Result<Vec<String>, Box<dyn Error>> {
if packages.is_empty() {
return Ok(Vec::new());
}
let pkg_config = env::var_os("PKG_CONFIG").unwrap_or_else(|| OsString::from("pkg-config"));
let mut cmd = Command::new(pkg_config);
cmd.arg("--cflags");
cmd.args(packages);
cmd.stderr(Stdio::inherit());
let out = cmd.output()?;
if !out.status.success() {
let (status, stdout) = (out.status, String::from_utf8_lossy(&out.stdout));
return Err(format!("command {cmd:?} failed, {status:?}\nstdout: {stdout}").into());
}
let stdout = str::from_utf8(&out.stdout)?;
Ok(shell_words::split(stdout.trim())?)
}
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
struct Layout {
size: usize,
alignment: usize,
}
#[derive(Copy, Clone, Debug, Default, Eq, PartialEq)]
struct Results {
passed: usize,
failed: usize,
}
impl Results {
fn record_passed(&mut self) {
self.passed += 1;
}
fn record_failed(&mut self) {
self.failed += 1;
}
fn summary(&self) -> String {
format!("{} passed; {} failed", self.passed, self.failed)
}
fn expect_total_success(&self) {
if self.failed == 0 {
println!("OK: {}", self.summary());
} else {
panic!("FAILED: {}", self.summary());
};
}
}
#[test]
fn cross_validate_constants_with_c() {
let mut c_constants: Vec<(String, String)> = Vec::new();
for l in get_c_output("constant").unwrap().lines() {
let (name, value) = l.split_once(';').expect("Missing ';' separator");
c_constants.push((name.to_owned(), value.to_owned()));
}
let mut results = Results::default();
for ((rust_name, rust_value), (c_name, c_value)) in
RUST_CONSTANTS.iter().zip(c_constants.iter())
{
if rust_name != c_name {
results.record_failed();
eprintln!("Name mismatch:\nRust: {rust_name:?}\nC: {c_name:?}");
continue;
}
if rust_value != c_value {
results.record_failed();
eprintln!(
"Constant value mismatch for {rust_name}\nRust: {rust_value:?}\nC: {c_value:?}",
);
continue;
}
results.record_passed();
}
results.expect_total_success();
}
#[test]
fn cross_validate_layout_with_c() {
let mut c_layouts = Vec::new();
for l in get_c_output("layout").unwrap().lines() {
let (name, value) = l.split_once(';').expect("Missing first ';' separator");
let (size, alignment) = value.split_once(';').expect("Missing second ';' separator");
let size = size.parse().expect("Failed to parse size");
let alignment = alignment.parse().expect("Failed to parse alignment");
c_layouts.push((name.to_owned(), Layout { size, alignment }));
}
let mut results = Results::default();
for ((rust_name, rust_layout), (c_name, c_layout)) in RUST_LAYOUTS.iter().zip(c_layouts.iter())
{
if rust_name != c_name {
results.record_failed();
eprintln!("Name mismatch:\nRust: {rust_name:?}\nC: {c_name:?}");
continue;
}
if rust_layout != c_layout {
results.record_failed();
eprintln!("Layout mismatch for {rust_name}\nRust: {rust_layout:?}\nC: {c_layout:?}",);
continue;
}
results.record_passed();
}
results.expect_total_success();
}
fn get_c_output(name: &str) -> Result<String, Box<dyn Error>> {
let tmpdir = Builder::new().prefix("abi").tempdir()?;
let exe = tmpdir.path().join(name);
let c_file = Path::new("tests").join(name).with_extension("c");
let cc = Compiler::new().expect("configured compiler");
cc.compile(&c_file, &exe)?;
let mut cmd = Command::new(exe);
cmd.stderr(Stdio::inherit());
let out = cmd.output()?;
if !out.status.success() {
let (status, stdout) = (out.status, String::from_utf8_lossy(&out.stdout));
return Err(format!("command {cmd:?} failed, {status:?}\nstdout: {stdout}").into());
}
Ok(String::from_utf8(out.stdout)?)
}
const RUST_LAYOUTS: &[(&str, Layout)] = &[
(
"GeglAbyssPolicy",
Layout {
size: size_of::<GeglAbyssPolicy>(),
alignment: align_of::<GeglAbyssPolicy>(),
},
),
(
"GeglAccessMode",
Layout {
size: size_of::<GeglAccessMode>(),
alignment: align_of::<GeglAccessMode>(),
},
),
(
"GeglAudioFragment",
Layout {
size: size_of::<GeglAudioFragment>(),
alignment: align_of::<GeglAudioFragment>(),
},
),
(
"GeglAudioFragmentClass",
Layout {
size: size_of::<GeglAudioFragmentClass>(),
alignment: align_of::<GeglAudioFragmentClass>(),
},
),
(
"GeglBablVariant",
Layout {
size: size_of::<GeglBablVariant>(),
alignment: align_of::<GeglBablVariant>(),
},
),
(
"GeglBlitFlags",
Layout {
size: size_of::<GeglBlitFlags>(),
alignment: align_of::<GeglBlitFlags>(),
},
),
(
"GeglBufferIterator",
Layout {
size: size_of::<GeglBufferIterator>(),
alignment: align_of::<GeglBufferIterator>(),
},
),
(
"GeglBufferIteratorItem",
Layout {
size: size_of::<GeglBufferIteratorItem>(),
alignment: align_of::<GeglBufferIteratorItem>(),
},
),
(
"GeglBufferMatrix2",
Layout {
size: size_of::<GeglBufferMatrix2>(),
alignment: align_of::<GeglBufferMatrix2>(),
},
),
(
"GeglCachePolicy",
Layout {
size: size_of::<GeglCachePolicy>(),
alignment: align_of::<GeglCachePolicy>(),
},
),
(
"GeglColor",
Layout {
size: size_of::<GeglColor>(),
alignment: align_of::<GeglColor>(),
},
),
(
"GeglColorClass",
Layout {
size: size_of::<GeglColorClass>(),
alignment: align_of::<GeglColorClass>(),
},
),
(
"GeglCurve",
Layout {
size: size_of::<GeglCurve>(),
alignment: align_of::<GeglCurve>(),
},
),
(
"GeglCurveClass",
Layout {
size: size_of::<GeglCurveClass>(),
alignment: align_of::<GeglCurveClass>(),
},
),
(
"GeglDistanceMetric",
Layout {
size: size_of::<GeglDistanceMetric>(),
alignment: align_of::<GeglDistanceMetric>(),
},
),
(
"GeglDitherMethod",
Layout {
size: size_of::<GeglDitherMethod>(),
alignment: align_of::<GeglDitherMethod>(),
},
),
(
"GeglLookup",
Layout {
size: size_of::<GeglLookup>(),
alignment: align_of::<GeglLookup>(),
},
),
(
"GeglMapFlags",
Layout {
size: size_of::<GeglMapFlags>(),
alignment: align_of::<GeglMapFlags>(),
},
),
(
"GeglMatrix3",
Layout {
size: size_of::<GeglMatrix3>(),
alignment: align_of::<GeglMatrix3>(),
},
),
(
"GeglMetadataHashClass",
Layout {
size: size_of::<GeglMetadataHashClass>(),
alignment: align_of::<GeglMetadataHashClass>(),
},
),
(
"GeglMetadataInterface",
Layout {
size: size_of::<GeglMetadataInterface>(),
alignment: align_of::<GeglMetadataInterface>(),
},
),
(
"GeglMetadataIter",
Layout {
size: size_of::<GeglMetadataIter>(),
alignment: align_of::<GeglMetadataIter>(),
},
),
(
"GeglMetadataMap",
Layout {
size: size_of::<GeglMetadataMap>(),
alignment: align_of::<GeglMetadataMap>(),
},
),
(
"GeglMetadataStore",
Layout {
size: size_of::<GeglMetadataStore>(),
alignment: align_of::<GeglMetadataStore>(),
},
),
(
"GeglMetadataStoreClass",
Layout {
size: size_of::<GeglMetadataStoreClass>(),
alignment: align_of::<GeglMetadataStoreClass>(),
},
),
(
"GeglOrientation",
Layout {
size: size_of::<GeglOrientation>(),
alignment: align_of::<GeglOrientation>(),
},
),
(
"GeglPadType",
Layout {
size: size_of::<GeglPadType>(),
alignment: align_of::<GeglPadType>(),
},
),
(
"GeglParamSpecDouble",
Layout {
size: size_of::<GeglParamSpecDouble>(),
alignment: align_of::<GeglParamSpecDouble>(),
},
),
(
"GeglParamSpecEnum",
Layout {
size: size_of::<GeglParamSpecEnum>(),
alignment: align_of::<GeglParamSpecEnum>(),
},
),
(
"GeglParamSpecFormat",
Layout {
size: size_of::<GeglParamSpecFormat>(),
alignment: align_of::<GeglParamSpecFormat>(),
},
),
(
"GeglParamSpecInt",
Layout {
size: size_of::<GeglParamSpecInt>(),
alignment: align_of::<GeglParamSpecInt>(),
},
),
(
"GeglParamSpecSeed",
Layout {
size: size_of::<GeglParamSpecSeed>(),
alignment: align_of::<GeglParamSpecSeed>(),
},
),
(
"GeglPath",
Layout {
size: size_of::<GeglPath>(),
alignment: align_of::<GeglPath>(),
},
),
(
"GeglPathItem",
Layout {
size: size_of::<GeglPathItem>(),
alignment: align_of::<GeglPathItem>(),
},
),
(
"GeglPathList",
Layout {
size: size_of::<GeglPathList>(),
alignment: align_of::<GeglPathList>(),
},
),
(
"GeglPathPoint",
Layout {
size: size_of::<GeglPathPoint>(),
alignment: align_of::<GeglPathPoint>(),
},
),
(
"GeglRectangle",
Layout {
size: size_of::<GeglRectangle>(),
alignment: align_of::<GeglRectangle>(),
},
),
(
"GeglRectangleAlignment",
Layout {
size: size_of::<GeglRectangleAlignment>(),
alignment: align_of::<GeglRectangleAlignment>(),
},
),
(
"GeglResolutionUnit",
Layout {
size: size_of::<GeglResolutionUnit>(),
alignment: align_of::<GeglResolutionUnit>(),
},
),
(
"GeglSamplerType",
Layout {
size: size_of::<GeglSamplerType>(),
alignment: align_of::<GeglSamplerType>(),
},
),
(
"GeglSerializeFlag",
Layout {
size: size_of::<GeglSerializeFlag>(),
alignment: align_of::<GeglSerializeFlag>(),
},
),
(
"GeglSplitStrategy",
Layout {
size: size_of::<GeglSplitStrategy>(),
alignment: align_of::<GeglSplitStrategy>(),
},
),
(
"GeglTileBackend",
Layout {
size: size_of::<GeglTileBackend>(),
alignment: align_of::<GeglTileBackend>(),
},
),
(
"GeglTileBackendClass",
Layout {
size: size_of::<GeglTileBackendClass>(),
alignment: align_of::<GeglTileBackendClass>(),
},
),
(
"GeglTileCommand",
Layout {
size: size_of::<GeglTileCommand>(),
alignment: align_of::<GeglTileCommand>(),
},
),
(
"GeglTileCopyParams",
Layout {
size: size_of::<GeglTileCopyParams>(),
alignment: align_of::<GeglTileCopyParams>(),
},
),
(
"GeglTileHandler",
Layout {
size: size_of::<GeglTileHandler>(),
alignment: align_of::<GeglTileHandler>(),
},
),
(
"GeglTileHandlerClass",
Layout {
size: size_of::<GeglTileHandlerClass>(),
alignment: align_of::<GeglTileHandlerClass>(),
},
),
(
"GeglTileSource",
Layout {
size: size_of::<GeglTileSource>(),
alignment: align_of::<GeglTileSource>(),
},
),
(
"GeglTileSourceClass",
Layout {
size: size_of::<GeglTileSourceClass>(),
alignment: align_of::<GeglTileSourceClass>(),
},
),
];
const RUST_CONSTANTS: &[(&str, &str)] = &[
("(gint) GEGL_ABYSS_BLACK", "3"),
("(gint) GEGL_ABYSS_CLAMP", "1"),
("(gint) GEGL_ABYSS_LOOP", "2"),
("(gint) GEGL_ABYSS_NONE", "0"),
("(gint) GEGL_ABYSS_WHITE", "4"),
("(guint) GEGL_ACCESS_READ", "1"),
("(guint) GEGL_ACCESS_READWRITE", "3"),
("(guint) GEGL_ACCESS_WRITE", "2"),
("GEGL_AUTO_ROWSTRIDE", "0"),
("(gint) GEGL_BABL_VARIANT_ALPHA", "8"),
("(gint) GEGL_BABL_VARIANT_FLOAT", "0"),
("(gint) GEGL_BABL_VARIANT_LINEAR", "1"),
("(gint) GEGL_BABL_VARIANT_LINEAR_PREMULTIPLIED", "4"),
(
"(gint) GEGL_BABL_VARIANT_LINEAR_PREMULTIPLIED_IF_ALPHA",
"6",
),
("(gint) GEGL_BABL_VARIANT_NONLINEAR", "2"),
("(gint) GEGL_BABL_VARIANT_PERCEPTUAL", "3"),
("(gint) GEGL_BABL_VARIANT_PERCEPTUAL_PREMULTIPLIED", "5"),
(
"(gint) GEGL_BABL_VARIANT_PERCEPTUAL_PREMULTIPLIED_IF_ALPHA",
"7",
),
("(guint) GEGL_BLIT_CACHE", "1"),
("(guint) GEGL_BLIT_DEFAULT", "0"),
("(guint) GEGL_BLIT_DIRTY", "2"),
("(gint) GEGL_CACHE_POLICY_ALWAYS", "2"),
("(gint) GEGL_CACHE_POLICY_AUTO", "0"),
("(gint) GEGL_CACHE_POLICY_NEVER", "1"),
("GEGL_CH_BACK_CENTER", "256"),
("GEGL_CH_BACK_LEFT", "16"),
("GEGL_CH_BACK_RIGHT", "32"),
("GEGL_CH_FRONT_CENTER", "4"),
("GEGL_CH_FRONT_LEFT", "1"),
("GEGL_CH_FRONT_LEFT_OF_CENTER", "64"),
("GEGL_CH_FRONT_RIGHT", "2"),
("GEGL_CH_FRONT_RIGHT_OF_CENTER", "128"),
("GEGL_CH_LAYOUT_2POINT1", "11"),
("GEGL_CH_LAYOUT_2_1", "259"),
("GEGL_CH_LAYOUT_2_2", "1539"),
("GEGL_CH_LAYOUT_3POINT1", "15"),
("GEGL_CH_LAYOUT_4POINT0", "263"),
("GEGL_CH_LAYOUT_4POINT1", "271"),
("GEGL_CH_LAYOUT_5POINT0", "1543"),
("GEGL_CH_LAYOUT_5POINT0_BACK", "55"),
("GEGL_CH_LAYOUT_5POINT1", "1551"),
("GEGL_CH_LAYOUT_5POINT1_BACK", "63"),
("GEGL_CH_LAYOUT_6POINT0", "1799"),
("GEGL_CH_LAYOUT_6POINT0_FRONT", "1731"),
("GEGL_CH_LAYOUT_6POINT1", "1807"),
("GEGL_CH_LAYOUT_6POINT1_BACK", "319"),
("GEGL_CH_LAYOUT_6POINT1_FRONT", "1739"),
("GEGL_CH_LAYOUT_7POINT0", "1591"),
("GEGL_CH_LAYOUT_7POINT0_FRONT", "1735"),
("GEGL_CH_LAYOUT_7POINT1", "1599"),
("GEGL_CH_LAYOUT_7POINT1_WIDE", "1743"),
("GEGL_CH_LAYOUT_7POINT1_WIDE_BACK", "255"),
("GEGL_CH_LAYOUT_HEXADECAGONAL", "6442710839"),
("GEGL_CH_LAYOUT_HEXAGONAL", "311"),
("GEGL_CH_LAYOUT_NATIVE", "9223372036854775808"),
("GEGL_CH_LAYOUT_OCTAGONAL", "1847"),
("GEGL_CH_LAYOUT_QUAD", "51"),
("GEGL_CH_LAYOUT_STEREO", "3"),
("GEGL_CH_LAYOUT_STEREO_DOWNMIX", "1610612736"),
("GEGL_CH_LAYOUT_SURROUND", "7"),
("GEGL_CH_LOW_FREQUENCY", "8"),
("GEGL_CH_LOW_FREQUENCY_2", "34359738368"),
("GEGL_CH_SIDE_LEFT", "512"),
("GEGL_CH_SIDE_RIGHT", "1024"),
("GEGL_CH_STEREO_LEFT", "536870912"),
("GEGL_CH_STEREO_RIGHT", "1073741824"),
("GEGL_CH_SURROUND_DIRECT_LEFT", "8589934592"),
("GEGL_CH_SURROUND_DIRECT_RIGHT", "17179869184"),
("GEGL_CH_TOP_BACK_CENTER", "65536"),
("GEGL_CH_TOP_BACK_LEFT", "32768"),
("GEGL_CH_TOP_BACK_RIGHT", "131072"),
("GEGL_CH_TOP_CENTER", "2048"),
("GEGL_CH_TOP_FRONT_CENTER", "8192"),
("GEGL_CH_TOP_FRONT_LEFT", "4096"),
("GEGL_CH_TOP_FRONT_RIGHT", "16384"),
("GEGL_CH_WIDE_LEFT", "2147483648"),
("GEGL_CH_WIDE_RIGHT", "4294967296"),
("(gint) GEGL_DISTANCE_METRIC_CHEBYSHEV", "2"),
("(gint) GEGL_DISTANCE_METRIC_EUCLIDEAN", "0"),
("(gint) GEGL_DISTANCE_METRIC_MANHATTAN", "1"),
("(gint) GEGL_DITHER_ARITHMETIC_ADD", "5"),
("(gint) GEGL_DITHER_ARITHMETIC_ADD_COVARIANT", "6"),
("(gint) GEGL_DITHER_ARITHMETIC_XOR", "7"),
("(gint) GEGL_DITHER_ARITHMETIC_XOR_COVARIANT", "8"),
("(gint) GEGL_DITHER_BAYER", "2"),
("(gint) GEGL_DITHER_BLUE_NOISE", "9"),
("(gint) GEGL_DITHER_BLUE_NOISE_COVARIANT", "10"),
("(gint) GEGL_DITHER_FLOYD_STEINBERG", "1"),
("(gint) GEGL_DITHER_NONE", "0"),
("(gint) GEGL_DITHER_RANDOM", "3"),
("(gint) GEGL_DITHER_RANDOM_COVARIANT", "4"),
("GEGL_FLOAT_EPSILON", "0.000010"),
("GEGL_LOOKUP_MAX_ENTRIES", "819200"),
("(gint) GEGL_MAP_EXCLUDE_UNMAPPED", "1"),
("GEGL_MAX_AUDIO_CHANNELS", "8"),
("(gint) GEGL_ORIENTATION_HORIZONTAL", "0"),
("(gint) GEGL_ORIENTATION_VERTICAL", "1"),
("(guint) GEGL_PARAM_PAD_INPUT", "512"),
("(guint) GEGL_PARAM_PAD_OUTPUT", "256"),
("(gint) GEGL_RECTANGLE_ALIGNMENT_NEAREST", "2"),
("(gint) GEGL_RECTANGLE_ALIGNMENT_SUBSET", "0"),
("(gint) GEGL_RECTANGLE_ALIGNMENT_SUPERSET", "1"),
("(gint) GEGL_RESOLUTION_UNIT_DPI", "1"),
("(gint) GEGL_RESOLUTION_UNIT_DPM", "2"),
("(gint) GEGL_RESOLUTION_UNIT_NONE", "0"),
("(gint) GEGL_SAMPLER_CUBIC", "2"),
("(gint) GEGL_SAMPLER_LINEAR", "1"),
("(gint) GEGL_SAMPLER_LOHALO", "4"),
("(gint) GEGL_SAMPLER_NEAREST", "0"),
("(gint) GEGL_SAMPLER_NOHALO", "3"),
("(guint) GEGL_SERIALIZE_BAKE_ANIM", "8"),
("(guint) GEGL_SERIALIZE_INDENT", "4"),
("(guint) GEGL_SERIALIZE_TRIM_DEFAULTS", "1"),
("(guint) GEGL_SERIALIZE_VERSION", "2"),
("(gint) GEGL_SPLIT_STRATEGY_AUTO", "0"),
("(gint) GEGL_SPLIT_STRATEGY_HORIZONTAL", "1"),
("(gint) GEGL_SPLIT_STRATEGY_VERTICAL", "2"),
("(gint) GEGL_TILE_COPY", "9"),
("(gint) GEGL_TILE_EXIST", "4"),
("(gint) GEGL_TILE_FLUSH", "6"),
("(gint) GEGL_TILE_GET", "2"),
("(gint) GEGL_TILE_IDLE", "0"),
("(gint) GEGL_TILE_IS_CACHED", "3"),
("(gint) GEGL_TILE_LAST_COMMAND", "10"),
("(gint) GEGL_TILE_REFETCH", "7"),
("(gint) GEGL_TILE_REINIT", "8"),
("(gint) GEGL_TILE_SET", "1"),
("(gint) GEGL_TILE_VOID", "5"),
("(gint) _GEGL_TILE_LAST_0_4_8_COMMAND", "9"),
];