diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index 242f57c..06d768c 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -10,6 +10,7 @@ on: - 'tests/**' - 'Cargo.toml' - 'opus/**/*' + - 'build.rs' pull_request: types: [opened, synchronize, reopened, ready_for_review] branches: @@ -20,6 +21,7 @@ on: - 'tests/**' - 'Cargo.toml' - 'opus/**/*' + - 'build.rs' jobs: check: strategy: diff --git a/build.rs b/build.rs index d19667e..2861369 100644 --- a/build.rs +++ b/build.rs @@ -90,7 +90,29 @@ fn set_cmake_define_if_present(config: &mut cmake::Config, name: &str) { fn build() { const CURRENT_DIR: &str = "opus"; + //Disable LTO if someone tries to force it (e.g. Arch makepkg) + //This is necessary because cmake crate doesn't pass env variables at configure step, so we will + //adjust both configure variables and general build env (just in case) + fn fix_build_env(cmake: &mut cmake::Config) { + for (var, cmake_var) in [("CFLAGS", "CMAKE_C_FLAGS"), ("CXXFLAGS", "CMAKE_CXX_FLAGS")] { + if let Ok(value) = std::env::var(var) { + if value.contains("-flto") { + println!("cargo:warning=env::{var} contains LTO option. Overriding it..."); + let filtered: String = value + .split_whitespace() + .filter(|f| !f.starts_with("-flto")) + .collect::>() + .join(" "); + cmake + .configure_arg(format!("-D{cmake_var}={filtered}")) + .env(var, filtered); + } + } + } + } + let mut cmake = cmake::Config::new(CURRENT_DIR); + fix_build_env(&mut cmake); cmake.define("OPUS_INSTALL_PKG_CONFIG_MODULE", "OFF") .define("OPUS_INSTALL_CMAKE_CONFIG_MODULE", "OFF") //Defining these variables disable GNUInstallDirs so in addition to /lib @@ -100,7 +122,9 @@ fn build() { .define("CMAKE_INSTALL_INCLUDEDIR", "include") .define("CMAKE_INSTALL_OLDINCLUDEDIR", "include") .define("CMAKE_INSTALL_LIBDIR", "lib") - .define("CMAKE_TRY_COMPILE_TARGET_TYPE", "STATIC_LIBRARY"); + .define("CMAKE_TRY_COMPILE_TARGET_TYPE", "STATIC_LIBRARY") + //Ensure opus's CMake never enables LTO + .define("CMAKE_INTERPROCEDURAL_OPTIMIZATION", "off"); //Keep this up to date with Cargo.toml if cfg!(feature = "dred") { diff --git a/tests/version.rs b/tests/version.rs index ccaaa45..e692f41 100644 --- a/tests/version.rs +++ b/tests/version.rs @@ -1,5 +1,6 @@ -use opusic_sys::opus_get_version_string; +use opusic_sys::{opus_get_version_string, opus_decoder_create, opus_decoder_destroy}; +use core::ptr; use core::ffi::CStr; #[test] @@ -10,3 +11,14 @@ fn check_version() { let version = version.to_str().expect("utf-8 string"); assert_eq!("libopus 1.6.1", version); } + +#[test] +fn check_decoder_symbols() { + let result = unsafe { + opus_decoder_create(8000, 2, ptr::null_mut()) + }; + assert!(!result.is_null()); + unsafe { + opus_decoder_destroy(result); + } +}