Skip to content

Commit

Permalink
C APIのテストにvoicevox_core.hを使う (#774)
Browse files Browse the repository at this point in the history
* C APIのテストにvoicevox_core.hを使う

* `$OUT_DIR`下に`cargo:rerun-if-changed`をかけない

* compatible_engine.hからvoicevox_core.hを`#include`する

* compatible_engine.hから、不要かつ今存在しないはずの関数を削除

* `cargo update -p clang-sys --precise 1.6.0`

* fixup! compatible_engine.hから、不要かつ今存在しないはずの関数を削除
  • Loading branch information
qryxip authored Apr 30, 2024
1 parent 62fa2dd commit e3cb7be
Show file tree
Hide file tree
Showing 18 changed files with 286 additions and 597 deletions.
65 changes: 53 additions & 12 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ anyhow = "1.0.65"
assert_cmd = "2.0.8"
async-std = "1.12.0"
async_zip = "=0.0.16"
bindgen = "0.69.4"
binstall-tar = "0.4.39"
bytes = "1.1.0"
camino = "1.1.6"
Expand Down
5 changes: 4 additions & 1 deletion crates/test_util/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ edition.workspace = true
[dependencies]
async_zip = { workspace = true, features = ["deflate"] }
futures-lite.workspace = true
libloading.workspace = true
once_cell.workspace = true
serde = { workspace = true, features = ["derive"] }
serde_json.workspace = true
Expand All @@ -13,6 +14,8 @@ tokio = { workspace = true, features = ["fs", "io-util", "sync"] }
[build-dependencies]
anyhow.workspace = true
async-std = { workspace = true, features = ["attributes"] }
bindgen.workspace = true
camino.workspace = true
flate2.workspace = true
fs-err.workspace = true
serde = { workspace = true, features = ["derive"] }
Expand All @@ -21,5 +24,5 @@ surf.workspace = true
tar.workspace = true

[lints.rust]
unsafe_code = "forbid"
unsafe_code = "allow" # C APIのbindgen
rust_2018_idioms = "warn"
21 changes: 20 additions & 1 deletion crates/test_util/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use std::{

use anyhow::ensure;
use async_std::io::ReadExt as _;
use camino::Utf8PathBuf;
use flate2::read::GzDecoder;
use tar::Archive;

Expand All @@ -28,7 +29,8 @@ async fn main() -> anyhow::Result<()> {

println!("cargo:rerun-if-changed=build.rs");
println!("cargo:rerun-if-changed=src/typing.rs");
Ok(())

generate_c_api_rs_bindings()
}

/// OpenJTalkの辞書をダウンロードして展開する。
Expand Down Expand Up @@ -120,3 +122,20 @@ fn generate_example_data_json(dist: &Path) -> anyhow::Result<()> {

Ok(())
}

fn generate_c_api_rs_bindings() -> anyhow::Result<()> {
static C_BINDINGS_PATH: &str = "../voicevox_core_c_api/include/voicevox_core.h";
static ADDITIONAL_C_BINDINGS_PATH: &str = "./compatible_engine.h";

let out_dir = Utf8PathBuf::from(env::var("OUT_DIR").unwrap());
bindgen::Builder::default()
.header(C_BINDINGS_PATH)
.header(ADDITIONAL_C_BINDINGS_PATH)
.parse_callbacks(Box::new(bindgen::CargoCallbacks::new()))
.dynamic_library_name("CApi")
.generate()?
.write_to_file(out_dir.join("c_api.rs"))?;
println!("cargo:rerun-if-changed={C_BINDINGS_PATH}");
println!("cargo:rerun-if-changed={ADDITIONAL_C_BINDINGS_PATH}");
Ok(())
}
28 changes: 28 additions & 0 deletions crates/test_util/compatible_engine.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#include <stdint.h>

bool initialize(bool use_gpu, int cpu_num_threads, bool load_all_models);

bool load_model(int64_t speaker_id);

bool is_model_loaded(int64_t speaker_id);

void finalize();

const char *metas();

const char *supported_devices();

bool yukarin_s_forward(int64_t length, int64_t *phoneme_list,
int64_t *speaker_id, float *output);

bool yukarin_sa_forward(int64_t length, int64_t *vowel_phoneme_list,
int64_t *consonant_phoneme_list,
int64_t *start_accent_list, int64_t *end_accent_list,
int64_t *start_accent_phrase_list,
int64_t *end_accent_phrase_list, int64_t *speaker_id,
float *output);

bool decode_forward(int64_t length, int64_t phoneme_size, float *f0,
float *phoneme, int64_t *speaker_id, float *output);

const char *last_error_message();
12 changes: 12 additions & 0 deletions crates/test_util/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
mod typing;

#[allow(
non_camel_case_types,
non_snake_case,
non_upper_case_globals,
unused_extern_crates,
clippy::missing_safety_doc,
clippy::too_many_arguments
)]
pub mod c_api {
include!(concat!(env!("OUT_DIR"), "/c_api.rs"));
}

use async_zip::{base::write::ZipFileWriter, Compression, ZipEntryBuilder};
use futures_lite::AsyncWriteExt as _;
use once_cell::sync::Lazy;
Expand Down
1 change: 0 additions & 1 deletion crates/voicevox_core_c_api/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ ndarray-stats.workspace = true
regex.workspace = true
serde = { workspace = true, features = ["derive"] }
serde_with.workspace = true
strum = { workspace = true, features = ["derive"] }
tempfile.workspace = true
test_util.workspace = true
toml.workspace = true
Expand Down
4 changes: 2 additions & 2 deletions crates/voicevox_core_c_api/tests/e2e/assert_cdylib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ pub(crate) fn exec<C: TestContext>() -> anyhow::Result<()> {
let exec_c_api_e2e_test = serde_json::from_str::<Box<dyn TestCase>>(&exec_c_api_e2e_test)?;

return unsafe {
let lib = &Library::new(C::cdylib_path())?;
let lib = Library::new(C::cdylib_path())?;
exec_c_api_e2e_test.exec(lib)
};
}
Expand Down Expand Up @@ -113,7 +113,7 @@ pub(crate) trait TestCase: Send {
///
/// `exec`は独立したプロセスで実行されるため、stdout/stderrへの出力をしたりグローバルな状態に
/// 依存してもよい。
unsafe fn exec(&self, lib: &Library) -> anyhow::Result<()>;
unsafe fn exec(&self, lib: Library) -> anyhow::Result<()>;

/// 別プロセスで実行された`exec`の結果をチェックする。
fn assert_output(&self, output: Utf8Output) -> AssertResult;
Expand Down
5 changes: 1 addition & 4 deletions crates/voicevox_core_c_api/tests/e2e/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,11 @@ mod assert_cdylib;
mod float_assert;
mod log_mask;
mod snapshots;
mod symbols;
mod testcases;

// voicevox_core_c_apiのcdylibを対象にテストを行う。
//
// C APIの定義を変更する場合:
// 1. symbols.rsの実装を変更する。
// 2. 1.によってコンパイルが通らなくなったら、適宜修正する。
// C APIの定義を変更した場合は、テスト実行前に`cargo xtask update-c-header`を実行すること。
//
// テストを追加する場合:
// 1. testcases/{テスト名}.rsを追加し、testcases.rsでマウントする。
Expand Down
Loading

0 comments on commit e3cb7be

Please sign in to comment.