From aefc6e76a4b6a8e3065f03370cd30dcb82e3d0f0 Mon Sep 17 00:00:00 2001 From: Brian Carroll Date: Fri, 10 Dec 2021 18:33:06 +0000 Subject: [PATCH] Build a Wasm libc.a from test_gen/build.rs --- compiler/test_gen/build.rs | 50 +++++++++++++++++-- .../test_gen/src/helpers/dummy_libc_program.c | 8 +++ 2 files changed, 53 insertions(+), 5 deletions(-) create mode 100644 compiler/test_gen/src/helpers/dummy_libc_program.c diff --git a/compiler/test_gen/build.rs b/compiler/test_gen/build.rs index 2c64270c66..8b3bc60810 100644 --- a/compiler/test_gen/build.rs +++ b/compiler/test_gen/build.rs @@ -4,15 +4,22 @@ use std::path::Path; use std::process::Command; pub const PLATFORM_FILENAME: &str = "wasm_test_platform"; -pub const DIRNAME_VAR: &str = "TEST_GEN_OUT"; +pub const OUT_DIR_VAR: &str = "TEST_GEN_OUT"; +pub const LIBC_PATH_VAR: &str = "TEST_GEN_WASM_LIBC_PATH"; fn main() { println!("cargo:rerun-if-changed=build.rs"); - println!("cargo:rerun-if-changed=src/helpers/{}.c", PLATFORM_FILENAME); let out_dir = env::var("OUT_DIR").unwrap(); - println!("cargo:rustc-env={}={}", DIRNAME_VAR, out_dir); + println!("cargo:rustc-env={}={}", OUT_DIR_VAR, out_dir); + + build_wasm_test_platform(&out_dir); + build_wasm_libc(&out_dir); +} + +fn build_wasm_test_platform(out_dir: &str) { + println!("cargo:rerun-if-changed=src/helpers/{}.c", PLATFORM_FILENAME); run_command( Path::new("."), @@ -28,7 +35,40 @@ fn main() { ); } -fn run_command>(path: P, command_str: &str, args: I) +fn build_wasm_libc(out_dir: &str) { + let source_path = "src/helpers/dummy_libc_program.c"; + println!("cargo:rerun-if-changed={}", source_path); + let cwd = Path::new("."); + + run_command( + cwd, + "zig", + [ + "build-exe", // must be an executable or it won't compile libc + "-target", + "wasm32-wasi", + "-lc", + source_path, + "-femit-bin=/dev/null", + "--global-cache-dir", + out_dir, + ], + ); + + let libc_path = run_command( + cwd, + "find", + [ + out_dir, + "-name", + "libc.a" + ], + ); + + println!("cargo:rustc-env={}={}", LIBC_PATH_VAR, libc_path); +} + +fn run_command>(path: P, command_str: &str, args: I) -> String where I: IntoIterator, S: AsRef, @@ -39,7 +79,7 @@ where .output(); match output_result { Ok(output) => match output.status.success() { - true => (), + true => std::str::from_utf8(&output.stdout), false => { let error_str = match std::str::from_utf8(&output.stderr) { Ok(stderr) => stderr.to_string(), diff --git a/compiler/test_gen/src/helpers/dummy_libc_program.c b/compiler/test_gen/src/helpers/dummy_libc_program.c new file mode 100644 index 0000000000..79ecd9ce07 --- /dev/null +++ b/compiler/test_gen/src/helpers/dummy_libc_program.c @@ -0,0 +1,8 @@ +#include + +void main() { + printf("Hello, I am a C program and I use libc.\n"); + printf("Would you like to use libc too, but just can't find the right path?\n"); + printf("Well, simply compile me from build.rs using Zig's --global-cache-dir,\n"); + printf("and have libc.a delivered right to your build directory!\n"); +}