Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support wasm32-unknown-emscripten #16

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .github/actions/auto_gen_bind_pr/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ runs:
shell: bash
run: echo "branch=${GITHUB_REF#refs/heads/}" >> $GITHUB_OUTPUT
id: extract_branch
- name: Install emscripten
if: inputs.triple == 'wasm32-unknown-emscripten'
uses: mymindstorm/setup-emsdk@v12
- name: Install cross compiler for linux aarch64
if: inputs.triple == 'aarch64-unknown-linux-gnu'
shell: bash
Expand Down
2 changes: 2 additions & 0 deletions .github/workflows/gen_bind.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ jobs:
triple: aarch64-linux-android
- os: ubuntu-latest
triple: x86_64-linux-android
- os: ubuntu-latest
triple: wasm32-unknown-emscripten
- os: macos-latest
triple: x86_64-apple-darwin
- os: macos-latest
Expand Down
5 changes: 5 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ jobs:
target: aarch64-linux-android
- os: ubuntu-latest
target: x86_64-linux-android
- os: ubuntu-latest
target: wasm32-unknown-emscripten
- os: macos-latest
target: aarch64-apple-darwin
- os: macos-latest
Expand All @@ -71,6 +73,9 @@ jobs:
steps:
- uses: actions/checkout@v2
# FIXME: auto_gen_bind_prにも処理があるので共通化する
- name: Install emscripten
if: matrix.target == 'wasm32-unknown-emscripten'
uses: mymindstorm/setup-emsdk@v12
- name: Install cross compiler for linux aarch64
if: matrix.target == 'aarch64-unknown-linux-gnu'
shell: bash
Expand Down
41 changes: 39 additions & 2 deletions crates/open_jtalk-sys/build.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
use std::{
env,
env, fs,
io::BufRead,
path::{Path, PathBuf},
process::Command,
process::{Command, Stdio},
str,
};

fn main() {
let mut cmake_conf = cmake::Config::new("open_jtalk");
let target = env::var("TARGET").unwrap();
Expand Down Expand Up @@ -60,6 +62,10 @@ fn main() {
}
}

if target.contains("emscripten") {
include_dirs.extend(search_emscripten_include_directories());
}

let dst_dir = cmake_conf.build();
let lib_dir = dst_dir.join("lib");
println!("cargo:rustc-link-search={}", lib_dir.to_str().unwrap());
Expand All @@ -85,6 +91,7 @@ fn generate_bindings(
.into_iter()
.map(|dir| format!("-I{}", dir.as_ref().to_str().unwrap()))
.chain([format!("-I{}", include_dir.to_str().unwrap())])
.chain(["-fvisibility=default".to_string()])
.collect::<Vec<_>>();
println!("cargo:rerun-if-changed=wrapper.hpp");
println!("cargo:rerun-if-changed=src/generated/bindings.rs");
Expand Down Expand Up @@ -118,3 +125,33 @@ fn generate_bindings(
.write_to_file(&generated_file)
.expect("Couldn't write bindings!");
}

fn search_emscripten_include_directories() -> impl IntoIterator<Item = PathBuf> {
let empty_cpp_path = Path::new(&env::var_os("OUT_DIR").unwrap()).join("empty.cpp");
fs::write(&empty_cpp_path, b"").unwrap();

let mut command;
if cfg!(target_os = "windows") {
command = Command::new("cmd");
command.arg("/C");
} else {
command = Command::new("sh");
command.arg("-c");
};

let empp_output = command
.arg(format!("em++ --verbose {}", empty_cpp_path.display()))
.stderr(Stdio::piped())
.output()
.unwrap();

empp_output
.stderr
.lines()
.map(Result::unwrap)
.skip_while(|line| line.trim() != "#include <...> search starts here:")
.skip(1)
.take_while(|line| line.trim() != "End of search list.")
.map(|line| PathBuf::from(line.trim()))
.collect::<Vec<_>>()
}
6 changes: 6 additions & 0 deletions crates/open_jtalk-sys/src/generated/bindings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,9 @@ include!(concat!(
env!("CARGO_MANIFEST_DIR"),
"/src/generated/android/aarch64/bindings.rs"
));

#[cfg(all(target_os = "emscripten", target_arch = "wasm32"))]
include!(concat!(
env!("CARGO_MANIFEST_DIR"),
"/src/generated/emscripten/wasm32/bindings.rs"
));
Loading