Skip to content

Commit

Permalink
Change: C APIをwasmで使うようにする
Browse files Browse the repository at this point in the history
  • Loading branch information
sevenc-nanashi committed Mar 15, 2024
1 parent 0c4d68b commit cfdf5f4
Show file tree
Hide file tree
Showing 33 changed files with 153 additions and 444 deletions.
73 changes: 7 additions & 66 deletions Cargo.lock

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

5 changes: 2 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,8 @@ rev = "ebb9dcb9b26ee681889b52b6db3b4f642b04a250"
# git = "https://github.com/VOICEVOX/open_jtalk-rs.git"
# rev = "70c76bb54522830e92803038191bf533ba68ce85"

# https://github.com/White-Green/open_jtalk-rs/tree/support_wasm32-unknown-emscripten
git = "https://github.com/White-Green/open_jtalk-rs.git"
branch = "support_wasm32-unknown-emscripten"
git = "https://github.com/sevenc-nanashi/open_jtalk-rs.git"
branch = "add/wasm"

# FIXME: iOS対応のpull request(https://github.com/wesleywiser/process_path/pull/16)がマージされる見込みが無いため
[workspace.dependencies.process_path]
Expand Down
7 changes: 6 additions & 1 deletion crates/voicevox_core_c_api/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,16 @@ futures.workspace = true
itertools.workspace = true
libc.workspace = true
once_cell.workspace = true
process_path.workspace = true
serde_json = { workspace = true, features = ["preserve_order"] }
thiserror.workspace = true
tracing.workspace = true
tracing-subscriber = { workspace = true, features = ["env-filter"] }
uuid.workspace = true
voicevox_core.workspace = true

[target."cfg(not(target_family = \"wasm\"))".dependencies]
process_path.workspace = true

[dev-dependencies]
anyhow.workspace = true
assert_cmd = { workspace = true, features = ["color-auto"] }
Expand All @@ -55,6 +57,9 @@ test_util.workspace = true
toml.workspace = true
typetag.workspace = true

[target."cfg(target_family = \"wasm\")".build-dependencies]
regex.workspace = true

[lints.rust]
unsafe_code = "allow" # C APIのための操作
rust_2018_idioms = "warn"
48 changes: 48 additions & 0 deletions crates/voicevox_core_c_api/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,52 @@ fn main() {
println!("cargo:rustc-link-arg=-Wl,-rpath,@loader_path/");
println!("cargo:rustc-link-arg=-Wl,-install_name,@rpath/libvoicevox_core.dylib");
}

if std::env::var("TARGET").unwrap() == "wasm32-unknown-emscripten" {
// use regex::Regex;
// use std::fs;

// let lib_rs = fs::read_to_string("src/lib.rs").unwrap();
// let re = Regex::new(r#"pub (?:unsafe )?extern "C" fn (\w+)"#).unwrap();
// let mut functions = vec![];
// for cap in re.captures_iter(&lib_rs) {
// functions.push(cap[1].to_string());
// }
// println!("cargo:rustc-link-arg=-sERROR_ON_UNDEFINED_SYMBOLS=0");
// // TODO: WARNにしたいけど、これにするとemccがクラッシュする(どうして...)
// // println!("cargo:rustc-link-arg=-sWARN_ON_UNDEFINED_SYMBOLS=1");
// println!(
// "cargo:rustc-link-arg=-sEXPORTED_FUNCTIONS=['{}']",
// functions.join("','")
// );
// println!("cargo:rustc-link-arg=-sEXPORTED_RUNTIME_METHODS=['ccall']");
// println!("cargo:rustc-link-arg=-sEXPORT_NAME=\"RawVoicevoxCore\"");
// println!("cargo:rustc-link-arg=-sMODULARIZE=1");
// println!("cargo:rustc-link-arg=-sALLOW_MEMORY_GROWTH=1");
// println!("cargo:rustc-link-arg=-sTOTAL_STACK=128MB");
// println!("cargo:rustc-link-arg=-sINITIAL_MEMORY=256MB");
println!(
"cargo:rustc-link-arg=--js-library={}",
std::env::var("CARGO_MANIFEST_DIR").unwrap() + "/wasm_library.js"
);

println!("cargo:rustc-link-arg=-sEXPORTED_RUNTIME_METHODS=['ccall', 'FS']");
// println!("cargo:rustc-link-arg=--no-entry");
let out_dir = std::path::PathBuf::from(std::env::var("OUT_DIR").unwrap());
let target_dir = out_dir
.parent()
.unwrap()
.parent()
.unwrap()
.parent()
.unwrap();
println!(
"cargo:rustc-link-arg=-o{}/voicevox_core_wasm_api.mjs",
target_dir.display()
);
println!("cargo:rustc-link-arg=-sERROR_ON_UNDEFINED_SYMBOLS=0");
println!("cargo:rustc-link-arg=-sEXPORT_NAME=VoicevoxCore");
println!("cargo:rustc-link-arg=-DEMSCRIPTEN_STANDALONE_WASM");
println!("cargo:rustc-link-arg=--no-entry");
}
}
22 changes: 15 additions & 7 deletions crates/voicevox_core_c_api/src/compatible_engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use std::{
collections::BTreeMap,
env,
ffi::{c_char, CString},
path::PathBuf,
sync::{Mutex, MutexGuard},
};

Expand Down Expand Up @@ -64,15 +65,22 @@ static VOICE_MODEL_SET: Lazy<VoiceModelSet> = Lazy::new(|| {
///
/// 失敗したらパニックする
fn get_all_models() -> Vec<voicevox_core::blocking::VoiceModel> {
let root_dir = if let Some(root_dir) = env::var_os(ROOT_DIR_ENV_NAME) {
let root_dir: PathBuf = if let Some(root_dir) = env::var_os(ROOT_DIR_ENV_NAME) {
root_dir.into()
} else {
process_path::get_dylib_path()
.or_else(process_path::get_executable_path)
.unwrap()
.parent()
.unwrap_or_else(|| "".as_ref())
.join("model")
#[cfg(target_family = "wasm")]
{
unimplemented!()
}
#[cfg(not(target_family = "wasm"))]
{
process_path::get_dylib_path()
.or_else(process_path::get_executable_path)
.unwrap()
.parent()
.unwrap_or_else(|| "".as_ref())
.join("model")
}
};

root_dir
Expand Down
File renamed without changes.
22 changes: 0 additions & 22 deletions crates/voicevox_core_wasm_api/Cargo.toml

This file was deleted.

29 changes: 0 additions & 29 deletions crates/voicevox_core_wasm_api/README.md

This file was deleted.

28 changes: 0 additions & 28 deletions crates/voicevox_core_wasm_api/build.rs

This file was deleted.

3 changes: 0 additions & 3 deletions crates/voicevox_core_wasm_api/src/commands/mod.rs

This file was deleted.

20 changes: 0 additions & 20 deletions crates/voicevox_core_wasm_api/src/commands/model.rs

This file was deleted.

Loading

0 comments on commit cfdf5f4

Please sign in to comment.