use std::{ env, fs, io }; use std::process::Command; fn main() { let manifest_dir = env::var("CARGO_MANIFEST_DIR").unwrap(); let out_dir = env::var("OUT_DIR").unwrap(); let lua_src_dir = format!("{}/libs/src/lua", manifest_dir); let bin_dst_dir = out_dir; let make_platform = match &env::var("CARGO_CFG_TARGET_FAMILY").unwrap()[..] { "unix" => "posix", "windows" => "mingw", _ => panic!("Unknown target family") }; let make_result = Command::new("make") .arg(make_platform) .current_dir(&lua_src_dir) .output() .unwrap(); if !make_result.status.success() { panic!("Error building Lua: {}, stdout: {}, stderr: {}", make_result.status, String::from_utf8_lossy(&make_result.stdout), String::from_utf8_lossy(&make_result.stderr)); } copy_archive_to_bin(&lua_src_dir, &bin_dst_dir) .expect("Error copying `liblua.a` file"); delete_intermediate_build_files(&lua_src_dir) .expect("Something went wrong deleting and moving intermediate build files"); } fn copy_archive_to_bin(src: &str, dst: &str) -> io::Result<()> { fs::create_dir_all(dst)?; let lib_lua_src_file = format!("{}/liblua.a", src); let lib_lua_dst_file = format!("{}/liblua.a", dst); fs::copy(lib_lua_src_file, lib_lua_dst_file)?; Ok(()) } fn delete_intermediate_build_files(dir: &str) -> io::Result<()> { let o_files = fs::read_dir(dir)? .filter_map(Result::ok) .map(|file| file.path()) .filter(|path| path.extension().is_some()) .filter(|path| path.extension().unwrap() == "o"); for o_file in o_files { fs::remove_file(o_file)?; } fs::remove_file(format!("{}/liblua.a", dir))?; fs::remove_file(format!("{}/lua", dir))?; fs::remove_file(format!("{}/luac", dir))?; Ok(()) }