From d00b89786aec0448d11c50299d56d24a10a525c6 Mon Sep 17 00:00:00 2001 From: Tomasz Andrzejak Date: Sat, 22 Jun 2024 21:32:30 +0200 Subject: [PATCH] Enable building libmysofa without cmake --- Cargo.toml | 7 ++-- examples/renderer.rs | 14 +++---- libmysofa-sys/Cargo.toml | 11 +++++- libmysofa-sys/build.rs | 66 ++++++++++++++++++++++++++++++- libmysofa-sys/libmysofa | 2 +- libmysofa-sys/src/config.h | 9 +++++ libmysofa-sys/src/mysofa_export.h | 7 ++++ 7 files changed, 101 insertions(+), 15 deletions(-) create mode 100644 libmysofa-sys/src/config.h create mode 100644 libmysofa-sys/src/mysofa_export.h diff --git a/Cargo.toml b/Cargo.toml index 6a3329f..dff5cf5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -14,13 +14,14 @@ categories = ["algorithms", "filesystem", "multimedia::audio"] [features] default = ["dsp"] dsp = ["dep:realfft"] +cmake-build = ["ffi/cmake-build"] [workspace] members = ["libmysofa-sys"] [dependencies] ffi = { package = "libmysofa-sys", version = "0.1", path = "libmysofa-sys" } -realfft = {version = "3.2", optional = true} +realfft = {version = "3.3", optional = true} thiserror = "1" [dev-dependencies] @@ -28,9 +29,9 @@ anyhow = "1.0" assert_approx_eq = "1.1" hound = "3.5" cpal = "0.15" -criterion = "0.4" +criterion = "0.5" rand = "0.8" -ringbuf = "0.3" +ringbuf = "0.4" [[bench]] name = "renderer" diff --git a/examples/renderer.rs b/examples/renderer.rs index 595f380..e238bea 100644 --- a/examples/renderer.rs +++ b/examples/renderer.rs @@ -7,7 +7,7 @@ use hound::WavReader; use sofar::reader::{Filter, OpenOptions, Sofar}; use sofar::render::Renderer; -use ringbuf::HeapRb; +use ringbuf::{traits::*, HeapRb}; use std::sync::{Arc, Condvar, Mutex}; use std::{env, io::Read}; @@ -92,7 +92,7 @@ where let (mut producer, mut consumer) = ringbuf.split(); for _ in 0..BLOCK_LEN { - producer.push(0.0).unwrap(); + producer.try_push(0.0).unwrap(); } let stream = device.build_output_stream( @@ -111,7 +111,7 @@ where return; } - while data_samples >= consumer.len() { + while data_samples >= consumer.occupied_len() { let src = reader .samples::() .take(BLOCK_LEN) @@ -125,14 +125,14 @@ where .unwrap(); for (l, r) in Iterator::zip(left.iter(), right.iter()) { - producer.push(*l).unwrap(); - producer.push(*r).unwrap(); + producer.try_push(*l).unwrap(); + producer.try_push(*r).unwrap(); } } for dst in data.chunks_exact_mut(2) { - dst[0] = consumer.pop().unwrap(); - dst[1] = consumer.pop().unwrap(); + dst[0] = consumer.try_pop().unwrap(); + dst[1] = consumer.try_pop().unwrap(); } }, |err| eprintln!("An error occurred on stream: {}", err), diff --git a/libmysofa-sys/Cargo.toml b/libmysofa-sys/Cargo.toml index 254d452..945b77f 100644 --- a/libmysofa-sys/Cargo.toml +++ b/libmysofa-sys/Cargo.toml @@ -16,8 +16,15 @@ exclude = ["libmysofa/tests"] libz-sys = { version = "1.1", default-features = false, features = ["static", "libc"] } [build-dependencies] -cmake = "0.1" -system-deps = "6.1" +cc = "1.0.99" +system-deps = "7.0" +cmake = { version = "0.1", optional = true } + +[features] +default = [] + +# Build libmysofa with its CMake. Requires that CMake is installed on the system. +cmake-build = ["cmake"] [package.metadata.system-deps] libmysofa = "1" diff --git a/libmysofa-sys/build.rs b/libmysofa-sys/build.rs index 1b62f68..cc78b4f 100644 --- a/libmysofa-sys/build.rs +++ b/libmysofa-sys/build.rs @@ -1,10 +1,72 @@ -use cmake::Config; use system_deps::{BuildInternalClosureError, Library}; use std::env; +#[cfg(not(feature = "cmake-build"))] pub fn build_from_src(lib: &str, version: &str) -> Result { - let mut config = Config::new("libmysofa"); + let lib = lib.strip_prefix("lib").unwrap_or(lib); + let dst = std::path::PathBuf::from(env::var_os("OUT_DIR").unwrap()); + let z_root = env::var_os("DEP_Z_ROOT").unwrap(); + let z_inc = std::path::PathBuf::from(&z_root).join("include"); + let z_lib = std::path::PathBuf::from(&z_root).join("lib"); + + let mut cfg = cc::Build::new(); + cfg.std("gnu99") + .flag_if_supported("-Wno-sign-compare") + .flag_if_supported("-Wno-unused-parameter") + .flag_if_supported("-Wno-unused-but-set-variable") + .include(z_inc) + .include("libmysofa/src/hrtf") + .include("src") + .file("libmysofa/src/hrtf/cache.c") + .file("libmysofa/src/hdf/gunzip.c") + .file("libmysofa/src/hdf/gcol.c") + .file("libmysofa/src/hrtf/tools.c") + .file("libmysofa/src/hdf/superblock.c") + .file("libmysofa/src/hrtf/loudness.c") + .file("libmysofa/src/hrtf/lookup.c") + .file("libmysofa/src/hdf/btree.c") + .file("libmysofa/src/hrtf/minphase.c") + .file("libmysofa/src/hrtf/neighbors.c") + .file("libmysofa/src/hrtf/easy.c") + .file("libmysofa/src/hrtf/check.c") + .file("libmysofa/src/hrtf/kdtree.c") + .file("libmysofa/src/hrtf/spherical.c") + .file("libmysofa/src/hrtf/reader.c") + .file("libmysofa/src/hdf/fractalhead.c") + .file("libmysofa/src/hrtf/interpolate.c") + .file("libmysofa/src/hrtf/resample.c") + .file("libmysofa/src/hdf/dataobject.c") + .file("libmysofa/src/resampler/speex_resampler.c") + .compile(lib); + + Ok(Library { + name: lib.to_owned(), + version: version.to_owned(), + source: system_deps::Source::EnvVariables, + link_paths: vec![dst, z_lib.to_owned()], + libs: vec![ + system_deps::InternalLib { + name: lib.to_owned(), + is_static_available: true, + }, + system_deps::InternalLib { + name: "z".to_owned(), + is_static_available: true, + }, + ], + frameworks: Default::default(), + framework_paths: Default::default(), + include_paths: Default::default(), + defines: Default::default(), + ld_args: Default::default(), + statik: true, + }) +} + +#[cfg(feature = "cmake-build")] +pub fn build_from_src(lib: &str, version: &str) -> Result { + let mut config = cmake::Config::new("libmysofa"); let z_root = env::var_os("DEP_Z_ROOT").unwrap(); let dst = config diff --git a/libmysofa-sys/libmysofa b/libmysofa-sys/libmysofa index 0b6c965..da9e4ad 160000 --- a/libmysofa-sys/libmysofa +++ b/libmysofa-sys/libmysofa @@ -1 +1 @@ -Subproject commit 0b6c96541cbabcaf4ace1a0f48d3aef548be448d +Subproject commit da9e4adc619ee3d1ae5e68da3ed14aa5e60b3ec1 diff --git a/libmysofa-sys/src/config.h b/libmysofa-sys/src/config.h new file mode 100644 index 0000000..ecb1b5d --- /dev/null +++ b/libmysofa-sys/src/config.h @@ -0,0 +1,9 @@ +#ifndef _CONFIG_H +#define _CONFIG_H + +#define CMAKE_INSTALL_PREFIX "/usr/local" +#define CPACK_PACKAGE_VERSION_MAJOR 1 +#define CPACK_PACKAGE_VERSION_MINOR 3 +#define CPACK_PACKAGE_VERSION_PATCH 2 + +#endif // _CONFIG_H diff --git a/libmysofa-sys/src/mysofa_export.h b/libmysofa-sys/src/mysofa_export.h new file mode 100644 index 0000000..9d704df --- /dev/null +++ b/libmysofa-sys/src/mysofa_export.h @@ -0,0 +1,7 @@ +#ifndef MYSOFA_EXPORT_H_ +#define MYSOFA_EXPORT_H_ + +#define MYSOFA_EXPORT +#define MYSOFA_NO_EXPORT + +#endif // MYSOFA_EXPORT_H_