2022-03-01 04:28:01 +03:00
|
|
|
use std::env;
|
2022-06-19 17:21:59 +03:00
|
|
|
use std::path::PathBuf;
|
2022-02-06 13:06:38 +03:00
|
|
|
use std::process::Command;
|
|
|
|
|
|
|
|
use roc_builtins::bitcode;
|
2022-06-19 17:21:59 +03:00
|
|
|
use wasi_libc_sys::{WASI_COMPILER_RT_PATH, WASI_LIBC_PATH};
|
2022-02-06 13:06:38 +03:00
|
|
|
|
|
|
|
const PLATFORM_FILENAME: &str = "repl_platform";
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
println!("cargo:rerun-if-changed=build.rs");
|
2022-04-11 16:19:56 +03:00
|
|
|
let source_path = format!("src/{}.c", PLATFORM_FILENAME);
|
|
|
|
println!("cargo:rerun-if-changed={}", source_path);
|
2022-02-06 13:06:38 +03:00
|
|
|
|
2022-04-11 16:19:56 +03:00
|
|
|
// Zig can produce *either* an object containing relocations OR an object containing libc code
|
|
|
|
// But we want both, so we have to compile twice with different flags, then link them
|
|
|
|
|
|
|
|
// Create an object file with relocations
|
|
|
|
let out_dir = env::var("OUT_DIR").unwrap();
|
|
|
|
let platform_obj = build_wasm_platform(&out_dir, &source_path);
|
|
|
|
|
2022-07-14 18:58:43 +03:00
|
|
|
let mut pre_linked_binary_path = PathBuf::from(std::env::var("OUT_DIR").unwrap());
|
|
|
|
pre_linked_binary_path.extend(&["pre_linked_binary"]);
|
|
|
|
pre_linked_binary_path.set_extension("o");
|
2022-06-19 17:00:11 +03:00
|
|
|
|
2022-07-14 18:58:43 +03:00
|
|
|
let output = Command::new(&zig_executable())
|
2022-06-19 17:00:11 +03:00
|
|
|
.args([
|
|
|
|
"wasm-ld",
|
2022-08-01 17:50:38 +03:00
|
|
|
&bitcode::get_builtins_wasm32_obj_path(),
|
2022-06-19 18:19:40 +03:00
|
|
|
platform_obj.to_str().unwrap(),
|
2022-06-19 17:21:59 +03:00
|
|
|
WASI_COMPILER_RT_PATH,
|
|
|
|
WASI_LIBC_PATH,
|
2022-06-19 17:00:11 +03:00
|
|
|
"-o",
|
|
|
|
pre_linked_binary_path.to_str().unwrap(),
|
|
|
|
"--export-all",
|
|
|
|
"--no-entry",
|
|
|
|
"--relocatable",
|
|
|
|
])
|
|
|
|
.output()
|
|
|
|
.unwrap();
|
2022-07-14 18:58:43 +03:00
|
|
|
|
|
|
|
assert!(output.status.success(), "{:#?}", output);
|
|
|
|
assert!(output.stdout.is_empty(), "{:#?}", output);
|
|
|
|
assert!(output.stderr.is_empty(), "{:#?}", output);
|
2022-02-06 13:06:38 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
fn zig_executable() -> String {
|
|
|
|
match std::env::var("ROC_ZIG") {
|
|
|
|
Ok(path) => path,
|
|
|
|
Err(_) => "zig".into(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-19 18:19:40 +03:00
|
|
|
fn build_wasm_platform(out_dir: &str, source_path: &str) -> PathBuf {
|
|
|
|
let mut platform_obj = PathBuf::from(out_dir).join(PLATFORM_FILENAME);
|
|
|
|
platform_obj.set_extension("o");
|
2022-04-11 16:19:56 +03:00
|
|
|
|
2022-06-19 17:00:11 +03:00
|
|
|
Command::new(&zig_executable())
|
|
|
|
.args([
|
2022-04-11 16:19:56 +03:00
|
|
|
"build-lib",
|
|
|
|
"-target",
|
|
|
|
"wasm32-wasi",
|
|
|
|
"-lc",
|
|
|
|
source_path,
|
2022-06-19 18:19:40 +03:00
|
|
|
&format!("-femit-bin={}", platform_obj.to_str().unwrap()),
|
2022-06-19 17:00:11 +03:00
|
|
|
])
|
|
|
|
.output()
|
|
|
|
.unwrap();
|
2022-04-11 16:19:56 +03:00
|
|
|
|
|
|
|
platform_obj
|
|
|
|
}
|