Skip to content

Commit

Permalink
Build emscripten target with c++
Browse files Browse the repository at this point in the history
  • Loading branch information
bytedream committed Dec 6, 2023
1 parent eef17e8 commit 3060481
Showing 1 changed file with 28 additions and 2 deletions.
30 changes: 28 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ impl Build {
let include_dir = out_dir.join("include");

let source_dir_base = Path::new(env!("CARGO_MANIFEST_DIR"));
let source_dir = match version {
let mut source_dir = match version {
Lua51 => source_dir_base.join("lua-5.1.5"),
Lua52 => source_dir_base.join("lua-5.2.4"),
Lua53 => source_dir_base.join("lua-5.3.6"),
Expand Down Expand Up @@ -105,7 +105,33 @@ impl Build {
config.define("LUA_USE_WINDOWS", None);
}
_ if target.ends_with("emscripten") => {
config.define("LUA_USE_POSIX", None);
config
.define("LUA_USE_POSIX", None)
.cpp(true)
.flag("-fexceptions"); // Enable exceptions to be caught

let cpp_source_dir = out_dir.join("cpp_source");
if cpp_source_dir.exists() {
fs::remove_dir_all(&cpp_source_dir).unwrap();
}
fs::create_dir_all(&cpp_source_dir).unwrap();

for file in fs::read_dir(&source_dir).unwrap() {
let file = file.unwrap();
let filename = file.file_name().to_string_lossy().to_string();
let src_file = source_dir.join(file.file_name());
let dst_file = cpp_source_dir.join(file.file_name());

let mut content = fs::read(src_file).unwrap();
// ljumptab.h only contains definitions and will cause errors when wrapping with
// 'extern "C"'
if filename.ends_with(".h") && !["ljumptab.h"].contains(&filename.as_str()) {
content.splice(0..0, b"extern \"C\" {\n".to_vec());
content.extend(b"\n}".to_vec())
}
fs::write(dst_file, content).unwrap();
}
source_dir = cpp_source_dir
}
_ => panic!("don't know how to build Lua for {}", target),
};
Expand Down

0 comments on commit 3060481

Please sign in to comment.