#![cfg(unix)]
use alsaseq_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] = &["alsaseq"];
#[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)] = &[
(
"ALSASeqClientInfo",
Layout {
size: size_of::<ALSASeqClientInfo>(),
alignment: align_of::<ALSASeqClientInfo>(),
},
),
(
"ALSASeqClientInfoClass",
Layout {
size: size_of::<ALSASeqClientInfoClass>(),
alignment: align_of::<ALSASeqClientInfoClass>(),
},
),
(
"ALSASeqClientPool",
Layout {
size: size_of::<ALSASeqClientPool>(),
alignment: align_of::<ALSASeqClientPool>(),
},
),
(
"ALSASeqClientPoolClass",
Layout {
size: size_of::<ALSASeqClientPoolClass>(),
alignment: align_of::<ALSASeqClientPoolClass>(),
},
),
(
"ALSASeqClientType",
Layout {
size: size_of::<ALSASeqClientType>(),
alignment: align_of::<ALSASeqClientType>(),
},
),
(
"ALSASeqEventCntr",
Layout {
size: size_of::<ALSASeqEventCntr>(),
alignment: align_of::<ALSASeqEventCntr>(),
},
),
(
"ALSASeqEventError",
Layout {
size: size_of::<ALSASeqEventError>(),
alignment: align_of::<ALSASeqEventError>(),
},
),
(
"ALSASeqEventLengthMode",
Layout {
size: size_of::<ALSASeqEventLengthMode>(),
alignment: align_of::<ALSASeqEventLengthMode>(),
},
),
(
"ALSASeqEventPriorityMode",
Layout {
size: size_of::<ALSASeqEventPriorityMode>(),
alignment: align_of::<ALSASeqEventPriorityMode>(),
},
),
(
"ALSASeqEventTimeMode",
Layout {
size: size_of::<ALSASeqEventTimeMode>(),
alignment: align_of::<ALSASeqEventTimeMode>(),
},
),
(
"ALSASeqEventTstampMode",
Layout {
size: size_of::<ALSASeqEventTstampMode>(),
alignment: align_of::<ALSASeqEventTstampMode>(),
},
),
(
"ALSASeqEventType",
Layout {
size: size_of::<ALSASeqEventType>(),
alignment: align_of::<ALSASeqEventType>(),
},
),
(
"ALSASeqFilterAttrFlag",
Layout {
size: size_of::<ALSASeqFilterAttrFlag>(),
alignment: align_of::<ALSASeqFilterAttrFlag>(),
},
),
(
"ALSASeqPortAttrFlag",
Layout {
size: size_of::<ALSASeqPortAttrFlag>(),
alignment: align_of::<ALSASeqPortAttrFlag>(),
},
),
(
"ALSASeqPortCapFlag",
Layout {
size: size_of::<ALSASeqPortCapFlag>(),
alignment: align_of::<ALSASeqPortCapFlag>(),
},
),
(
"ALSASeqPortInfo",
Layout {
size: size_of::<ALSASeqPortInfo>(),
alignment: align_of::<ALSASeqPortInfo>(),
},
),
(
"ALSASeqPortInfoClass",
Layout {
size: size_of::<ALSASeqPortInfoClass>(),
alignment: align_of::<ALSASeqPortInfoClass>(),
},
),
(
"ALSASeqQuerySubscribeType",
Layout {
size: size_of::<ALSASeqQuerySubscribeType>(),
alignment: align_of::<ALSASeqQuerySubscribeType>(),
},
),
(
"ALSASeqQueueInfo",
Layout {
size: size_of::<ALSASeqQueueInfo>(),
alignment: align_of::<ALSASeqQueueInfo>(),
},
),
(
"ALSASeqQueueInfoClass",
Layout {
size: size_of::<ALSASeqQueueInfoClass>(),
alignment: align_of::<ALSASeqQueueInfoClass>(),
},
),
(
"ALSASeqQueueStatus",
Layout {
size: size_of::<ALSASeqQueueStatus>(),
alignment: align_of::<ALSASeqQueueStatus>(),
},
),
(
"ALSASeqQueueStatusClass",
Layout {
size: size_of::<ALSASeqQueueStatusClass>(),
alignment: align_of::<ALSASeqQueueStatusClass>(),
},
),
(
"ALSASeqQueueTempo",
Layout {
size: size_of::<ALSASeqQueueTempo>(),
alignment: align_of::<ALSASeqQueueTempo>(),
},
),
(
"ALSASeqQueueTempoClass",
Layout {
size: size_of::<ALSASeqQueueTempoClass>(),
alignment: align_of::<ALSASeqQueueTempoClass>(),
},
),
(
"ALSASeqQueueTimerAlsa",
Layout {
size: size_of::<ALSASeqQueueTimerAlsa>(),
alignment: align_of::<ALSASeqQueueTimerAlsa>(),
},
),
(
"ALSASeqQueueTimerAlsaClass",
Layout {
size: size_of::<ALSASeqQueueTimerAlsaClass>(),
alignment: align_of::<ALSASeqQueueTimerAlsaClass>(),
},
),
(
"ALSASeqQueueTimerCommonInterface",
Layout {
size: size_of::<ALSASeqQueueTimerCommonInterface>(),
alignment: align_of::<ALSASeqQueueTimerCommonInterface>(),
},
),
(
"ALSASeqQueueTimerType",
Layout {
size: size_of::<ALSASeqQueueTimerType>(),
alignment: align_of::<ALSASeqQueueTimerType>(),
},
),
(
"ALSASeqRemoveFilter",
Layout {
size: size_of::<ALSASeqRemoveFilter>(),
alignment: align_of::<ALSASeqRemoveFilter>(),
},
),
(
"ALSASeqRemoveFilterClass",
Layout {
size: size_of::<ALSASeqRemoveFilterClass>(),
alignment: align_of::<ALSASeqRemoveFilterClass>(),
},
),
(
"ALSASeqRemoveFilterFlag",
Layout {
size: size_of::<ALSASeqRemoveFilterFlag>(),
alignment: align_of::<ALSASeqRemoveFilterFlag>(),
},
),
(
"ALSASeqSpecificAddress",
Layout {
size: size_of::<ALSASeqSpecificAddress>(),
alignment: align_of::<ALSASeqSpecificAddress>(),
},
),
(
"ALSASeqSpecificClientId",
Layout {
size: size_of::<ALSASeqSpecificClientId>(),
alignment: align_of::<ALSASeqSpecificClientId>(),
},
),
(
"ALSASeqSpecificPortId",
Layout {
size: size_of::<ALSASeqSpecificPortId>(),
alignment: align_of::<ALSASeqSpecificPortId>(),
},
),
(
"ALSASeqSpecificQueueId",
Layout {
size: size_of::<ALSASeqSpecificQueueId>(),
alignment: align_of::<ALSASeqSpecificQueueId>(),
},
),
(
"ALSASeqSubscribeData",
Layout {
size: size_of::<ALSASeqSubscribeData>(),
alignment: align_of::<ALSASeqSubscribeData>(),
},
),
(
"ALSASeqSubscribeDataClass",
Layout {
size: size_of::<ALSASeqSubscribeDataClass>(),
alignment: align_of::<ALSASeqSubscribeDataClass>(),
},
),
(
"ALSASeqSystemInfo",
Layout {
size: size_of::<ALSASeqSystemInfo>(),
alignment: align_of::<ALSASeqSystemInfo>(),
},
),
(
"ALSASeqSystemInfoClass",
Layout {
size: size_of::<ALSASeqSystemInfoClass>(),
alignment: align_of::<ALSASeqSystemInfoClass>(),
},
),
(
"ALSASeqUserClient",
Layout {
size: size_of::<ALSASeqUserClient>(),
alignment: align_of::<ALSASeqUserClient>(),
},
),
(
"ALSASeqUserClientClass",
Layout {
size: size_of::<ALSASeqUserClientClass>(),
alignment: align_of::<ALSASeqUserClientClass>(),
},
),
(
"ALSASeqUserClientError",
Layout {
size: size_of::<ALSASeqUserClientError>(),
alignment: align_of::<ALSASeqUserClientError>(),
},
),
];
const RUST_CONSTANTS: &[(&str, &str)] = &[
("(gint) ALSASEQ_CLIENT_TYPE_KERNEL", "2"),
("(gint) ALSASEQ_CLIENT_TYPE_NONE", "0"),
("(gint) ALSASEQ_CLIENT_TYPE_USER", "1"),
("(gint) ALSASEQ_EVENT_ERROR_FAILED", "0"),
("(gint) ALSASEQ_EVENT_ERROR_INVALID_DATA_TYPE", "1"),
("(gint) ALSASEQ_EVENT_ERROR_INVALID_LENGTH_MODE", "2"),
("(gint) ALSASEQ_EVENT_ERROR_INVALID_TSTAMP_MODE", "3"),
("(gint) ALSASEQ_EVENT_LENGTH_MODE_FIXED", "0"),
("(gint) ALSASEQ_EVENT_LENGTH_MODE_POINTER", "8"),
("(gint) ALSASEQ_EVENT_LENGTH_MODE_VARIABLE", "4"),
("(gint) ALSASEQ_EVENT_PRIORITY_MODE_HIGH", "16"),
("(gint) ALSASEQ_EVENT_PRIORITY_MODE_NORMAL", "0"),
("(gint) ALSASEQ_EVENT_TIME_MODE_ABS", "0"),
("(gint) ALSASEQ_EVENT_TIME_MODE_REL", "2"),
("(gint) ALSASEQ_EVENT_TSTAMP_MODE_REAL", "1"),
("(gint) ALSASEQ_EVENT_TSTAMP_MODE_TICK", "0"),
("(gint) ALSASEQ_EVENT_TYPE_BOUNCE", "131"),
("(gint) ALSASEQ_EVENT_TYPE_CHANPRESS", "12"),
("(gint) ALSASEQ_EVENT_TYPE_CLIENT_CHANGE", "62"),
("(gint) ALSASEQ_EVENT_TYPE_CLIENT_EXIT", "61"),
("(gint) ALSASEQ_EVENT_TYPE_CLIENT_START", "60"),
("(gint) ALSASEQ_EVENT_TYPE_CLOCK", "36"),
("(gint) ALSASEQ_EVENT_TYPE_CONTINUE", "31"),
("(gint) ALSASEQ_EVENT_TYPE_CONTROL14", "14"),
("(gint) ALSASEQ_EVENT_TYPE_CONTROLLER", "10"),
("(gint) ALSASEQ_EVENT_TYPE_ECHO", "50"),
("(gint) ALSASEQ_EVENT_TYPE_KEYPRESS", "8"),
("(gint) ALSASEQ_EVENT_TYPE_KEYSIGN", "24"),
("(gint) ALSASEQ_EVENT_TYPE_NONE", "255"),
("(gint) ALSASEQ_EVENT_TYPE_NONREGPARAM", "15"),
("(gint) ALSASEQ_EVENT_TYPE_NOTE", "5"),
("(gint) ALSASEQ_EVENT_TYPE_NOTEOFF", "7"),
("(gint) ALSASEQ_EVENT_TYPE_NOTEON", "6"),
("(gint) ALSASEQ_EVENT_TYPE_OSS", "51"),
("(gint) ALSASEQ_EVENT_TYPE_PGMCHANGE", "11"),
("(gint) ALSASEQ_EVENT_TYPE_PITCHBEND", "13"),
("(gint) ALSASEQ_EVENT_TYPE_PORT_CHANGE", "65"),
("(gint) ALSASEQ_EVENT_TYPE_PORT_EXIT", "64"),
("(gint) ALSASEQ_EVENT_TYPE_PORT_START", "63"),
("(gint) ALSASEQ_EVENT_TYPE_PORT_SUBSCRIBED", "66"),
("(gint) ALSASEQ_EVENT_TYPE_PORT_UNSUBSCRIBED", "67"),
("(gint) ALSASEQ_EVENT_TYPE_QFRAME", "22"),
("(gint) ALSASEQ_EVENT_TYPE_QUEUE_SKEW", "38"),
("(gint) ALSASEQ_EVENT_TYPE_REGPARAM", "16"),
("(gint) ALSASEQ_EVENT_TYPE_RESET", "41"),
("(gint) ALSASEQ_EVENT_TYPE_RESULT", "1"),
("(gint) ALSASEQ_EVENT_TYPE_SENSING", "42"),
("(gint) ALSASEQ_EVENT_TYPE_SETPOS_TICK", "33"),
("(gint) ALSASEQ_EVENT_TYPE_SETPOS_TIME", "34"),
("(gint) ALSASEQ_EVENT_TYPE_SONGPOS", "20"),
("(gint) ALSASEQ_EVENT_TYPE_SONGSEL", "21"),
("(gint) ALSASEQ_EVENT_TYPE_START", "30"),
("(gint) ALSASEQ_EVENT_TYPE_STOP", "32"),
("(gint) ALSASEQ_EVENT_TYPE_SYSEX", "130"),
("(gint) ALSASEQ_EVENT_TYPE_SYSTEM", "0"),
("(gint) ALSASEQ_EVENT_TYPE_TEMPO", "35"),
("(gint) ALSASEQ_EVENT_TYPE_TICK", "37"),
("(gint) ALSASEQ_EVENT_TYPE_TIMESIGN", "23"),
("(gint) ALSASEQ_EVENT_TYPE_TUNE_REQUEST", "40"),
("(gint) ALSASEQ_EVENT_TYPE_USR0", "90"),
("(gint) ALSASEQ_EVENT_TYPE_USR1", "91"),
("(gint) ALSASEQ_EVENT_TYPE_USR2", "92"),
("(gint) ALSASEQ_EVENT_TYPE_USR3", "93"),
("(gint) ALSASEQ_EVENT_TYPE_USR4", "94"),
("(gint) ALSASEQ_EVENT_TYPE_USR5", "95"),
("(gint) ALSASEQ_EVENT_TYPE_USR6", "96"),
("(gint) ALSASEQ_EVENT_TYPE_USR7", "97"),
("(gint) ALSASEQ_EVENT_TYPE_USR8", "98"),
("(gint) ALSASEQ_EVENT_TYPE_USR9", "99"),
("(gint) ALSASEQ_EVENT_TYPE_USR_VAR0", "135"),
("(gint) ALSASEQ_EVENT_TYPE_USR_VAR1", "136"),
("(gint) ALSASEQ_EVENT_TYPE_USR_VAR2", "137"),
("(gint) ALSASEQ_EVENT_TYPE_USR_VAR3", "138"),
("(gint) ALSASEQ_EVENT_TYPE_USR_VAR4", "139"),
("(guint) ALSASEQ_FILTER_ATTR_FLAG_BOUNCE", "4"),
("(guint) ALSASEQ_FILTER_ATTR_FLAG_BROADCAST", "1"),
("(guint) ALSASEQ_FILTER_ATTR_FLAG_MULTICAST", "2"),
("(guint) ALSASEQ_PORT_ATTR_FLAG_APPLICATION", "1048576"),
("(guint) ALSASEQ_PORT_ATTR_FLAG_HARDWARE", "65536"),
("(guint) ALSASEQ_PORT_ATTR_FLAG_MIDI_GENERIC", "2"),
("(guint) ALSASEQ_PORT_ATTR_FLAG_MIDI_GM", "4"),
("(guint) ALSASEQ_PORT_ATTR_FLAG_MIDI_GM2", "64"),
("(guint) ALSASEQ_PORT_ATTR_FLAG_MIDI_GS", "8"),
("(guint) ALSASEQ_PORT_ATTR_FLAG_MIDI_MT32", "32"),
("(guint) ALSASEQ_PORT_ATTR_FLAG_MIDI_XG", "16"),
("(guint) ALSASEQ_PORT_ATTR_FLAG_PORT", "524288"),
("(guint) ALSASEQ_PORT_ATTR_FLAG_SOFTWARE", "131072"),
("(guint) ALSASEQ_PORT_ATTR_FLAG_SPECIFIC", "1"),
("(guint) ALSASEQ_PORT_ATTR_FLAG_SYNTHESIZER", "262144"),
("(guint) ALSASEQ_PORT_CAP_FLAG_DUPLEX", "16"),
("(guint) ALSASEQ_PORT_CAP_FLAG_NO_EXPORT", "128"),
("(guint) ALSASEQ_PORT_CAP_FLAG_READ", "1"),
("(guint) ALSASEQ_PORT_CAP_FLAG_SUBS_READ", "32"),
("(guint) ALSASEQ_PORT_CAP_FLAG_SUBS_WRITE", "64"),
("(guint) ALSASEQ_PORT_CAP_FLAG_WRITE", "2"),
("(gint) ALSASEQ_QUERY_SUBSCRIBE_TYPE_READ", "0"),
("(gint) ALSASEQ_QUERY_SUBSCRIBE_TYPE_WRITE", "1"),
("(gint) ALSASEQ_QUEUE_TIMER_TYPE_ALSA", "0"),
("(guint) ALSASEQ_REMOVE_FILTER_FLAG_DEST", "4"),
("(guint) ALSASEQ_REMOVE_FILTER_FLAG_DEST_CHANNEL", "8"),
("(guint) ALSASEQ_REMOVE_FILTER_FLAG_EVENT_TYPE", "128"),
("(guint) ALSASEQ_REMOVE_FILTER_FLAG_IGNORE_OFF", "256"),
("(guint) ALSASEQ_REMOVE_FILTER_FLAG_INPUT", "1"),
("(guint) ALSASEQ_REMOVE_FILTER_FLAG_OUTPUT", "2"),
("(guint) ALSASEQ_REMOVE_FILTER_FLAG_TAG_MATCH", "512"),
("(guint) ALSASEQ_REMOVE_FILTER_FLAG_TIME_AFTER", "32"),
("(guint) ALSASEQ_REMOVE_FILTER_FLAG_TIME_BEFORE", "16"),
("(guint) ALSASEQ_REMOVE_FILTER_FLAG_TIME_TICK", "64"),
("(gint) ALSASEQ_SPECIFIC_ADDRESS_BROADCAST", "255"),
("(gint) ALSASEQ_SPECIFIC_ADDRESS_SUBSCRIBERS", "254"),
("(gint) ALSASEQ_SPECIFIC_ADDRESS_UNKNOWN", "253"),
("(gint) ALSASEQ_SPECIFIC_CLIENT_ID_DUMMY", "14"),
("(gint) ALSASEQ_SPECIFIC_CLIENT_ID_OSS", "15"),
("(gint) ALSASEQ_SPECIFIC_CLIENT_ID_SYSTEM", "0"),
("(gint) ALSASEQ_SPECIFIC_PORT_ID_SYSTEM_ANNOUNCE", "1"),
("(gint) ALSASEQ_SPECIFIC_PORT_ID_SYSTEM_TIMER", "0"),
("(gint) ALSASEQ_SPECIFIC_QUEUE_ID_DIRECT", "253"),
("(gint) ALSASEQ_USER_CLIENT_ERROR_EVENT_UNDELIVERABLE", "3"),
("(gint) ALSASEQ_USER_CLIENT_ERROR_FAILED", "0"),
("(gint) ALSASEQ_USER_CLIENT_ERROR_PORT_PERMISSION", "1"),
("(gint) ALSASEQ_USER_CLIENT_ERROR_QUEUE_PERMISSION", "2"),
];