Skip to content

Commit 482d145

Browse files
author
Maxime Bedard
committed
Compile in debug mode by default
1 parent 15af793 commit 482d145

File tree

4 files changed

+93
-48
lines changed

4 files changed

+93
-48
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/target
1+
target/
22
javy.wat
33
javy.wasm
44
index.js

Makefile

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,31 +2,36 @@
22
.DEFAULT_GOAL := cli
33

44
cli: core
5-
cd crates/cli && cargo build --release
5+
cd crates/cli && cargo build && cd -
66

77
check-benchmarks:
88
cd crates/benchmarks \
9-
&& cargo check --benches --release
9+
&& cargo check --benches --release \
10+
&& cd -
1011

1112
core:
1213
cd crates/core \
13-
&& cargo build --release --target=wasm32-wasi
14+
&& cargo build --release --target=wasm32-wasi \
15+
&& cd -
1416

1517
tests: check-benchmarks core
18+
cd crates/cli \
19+
&& cargo test \
20+
&& cd -
1621

1722
fmt: fmt-quickjs-sys fmt-core fmt-cli
1823

1924
fmt-quickjs-sys:
2025
cd crates/quickjs-sys/ \
2126
&& cargo fmt -- --check \
2227
&& cargo clippy -- -D warnings \
23-
&& cd - \
28+
&& cd -
2429

2530
fmt-core:
2631
cd crates/core/ \
2732
&& cargo fmt -- --check \
2833
&& cargo clippy -- -D warnings \
29-
&& cd - \
34+
&& cd -
3035

3136
fmt-cli:
3237
cd crates/cli/ \

crates/cli/Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,7 @@ which = "4.2"
2323
structopt = "0.3"
2424
anyhow = "1.0"
2525
tempfile = "3.2.0"
26+
27+
[build-dependencies]
28+
which = "4.2"
29+

crates/cli/build.rs

Lines changed: 78 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
use std::env;
2-
use std::path::PathBuf;
2+
use std::fs;
3+
use std::io;
4+
use std::io::Write;
5+
use std::path::{Path, PathBuf};
6+
use std::process::Command;
37

48
fn main() {
59
copy_prebuilt_binaries();
@@ -14,56 +18,21 @@ fn main() {
1418
// When using clippy, we need to write a stubbed engine.wasm file to ensure compilation succeeds. This
1519
// skips building the actual engine.wasm binary that would be injected into the CLI binary.
1620
fn stub_engine_for_clippy() {
17-
let out_dir: PathBuf = env::var("OUT_DIR")
18-
.expect("failed to retrieve out dir")
19-
.into();
21+
let engine_path = PathBuf::from(env::var("OUT_DIR").unwrap())
22+
.join("engine.wasm");
2023

21-
let engine_path = out_dir.join("engine.wasm");
2224
if !engine_path.exists() {
23-
std::fs::write(engine_path, "").expect("failed to write empty engine.wasm stub");
25+
std::fs::write(engine_path, &[]).expect("failed to write empty engine.wasm stub");
2426
println!("cargo:warning=using stubbed engine.wasm for static analysis purposes...");
2527
}
2628
}
2729

28-
fn copy_engine_binary() {
29-
let profile = env::var("PROFILE").expect("Couldn't retrieve profile");
30-
if profile != "release" {
31-
eprintln!("only --release is supported due to https://github.com/bytecodealliance/wizer/issues/27");
32-
std::process::exit(1);
33-
}
34-
35-
let out_dir: PathBuf = env::var("OUT_DIR")
36-
.expect("failed to retrieve out dir")
37-
.into();
38-
39-
let engine_path: PathBuf = std::env::var("CARGO_MANIFEST_DIR")
40-
.map(PathBuf::from)
41-
.ok()
42-
.map(|mut b| {
43-
b.pop();
44-
b.pop();
45-
b.join("target")
46-
.join("wasm32-wasi")
47-
.join(profile)
48-
.join("javy_core.wasm")
49-
})
50-
.expect("failed to create path");
51-
52-
println!("cargo:rerun-if-changed={:?}", engine_path);
53-
54-
// Only copy the file when it exists. Cargo will take care of re-running this script when the file changes.
55-
if engine_path.exists() {
56-
std::fs::copy(&engine_path, out_dir.join("engine.wasm"))
57-
.unwrap_or_else(|_| panic!("failed to copy engine from {:?}", engine_path));
58-
}
59-
}
60-
6130
// Copy OS specific pre-built binaries to a known location. The binaries will be embedded in the final binary and
6231
// extracted to a temporary location if it's not already installed.
6332
fn copy_prebuilt_binaries() {
64-
let target_dir = PathBuf::from(std::env::var("OUT_DIR").unwrap());
65-
let target_os = PathBuf::from(std::env::var("CARGO_CFG_TARGET_OS").unwrap());
66-
let root = PathBuf::from(std::env::var("CARGO_MANIFEST_DIR").unwrap());
33+
let target_dir = PathBuf::from(env::var("OUT_DIR").unwrap());
34+
let target_os = PathBuf::from(env::var("CARGO_CFG_TARGET_OS").unwrap());
35+
let root = PathBuf::from(env::var("CARGO_MANIFEST_DIR").unwrap());
6736

6837
let vendor_dir = root.join("vendor").join(target_os);
6938
let target_vendor_dir = target_dir.join("vendor");
@@ -79,3 +48,70 @@ fn copy_prebuilt_binaries() {
7948
}
8049
});
8150
}
51+
52+
// Copy the engine binary build from the `core` crate, and run wasm-strip + wasm-opt against it as suggested by https://github.com/bytecodealliance/wizer/issues/27.
53+
fn copy_engine_binary() {
54+
let mut engine_path = PathBuf::from(env::var("CARGO_MANIFEST_DIR").unwrap());
55+
engine_path.pop();
56+
engine_path.pop();
57+
let engine_path = engine_path.join("target/wasm32-wasi/debug/javy_core.wasm");
58+
59+
println!("cargo:rerun-if-changed={:?}", engine_path);
60+
61+
if engine_path.exists() {
62+
let copied_engine_path = PathBuf::from(env::var("OUT_DIR").unwrap())
63+
.join("engine.wasm");
64+
65+
fs::copy(&engine_path, &copied_engine_path).unwrap();
66+
optimize_engine(&copied_engine_path);
67+
}
68+
}
69+
70+
fn optimize_engine(engine_path: impl AsRef<Path>) {
71+
if env::var("JAVY_SKIP_ENGINE_OPTIMIZATIONS").is_ok() {
72+
return;
73+
}
74+
75+
run_wasm_strip(&engine_path);
76+
run_wasm_opt(&engine_path);
77+
}
78+
79+
fn run_wasm_strip(engine_path: impl AsRef<Path>) {
80+
let wasm_strip = which::which("wasm-strip")
81+
.unwrap_or_else(|_| {
82+
PathBuf::from(env::var("OUT_DIR").unwrap())
83+
.join("vendor/wasm-opt")
84+
});
85+
86+
let output = Command::new(wasm_strip)
87+
.arg(engine_path.as_ref())
88+
.output()
89+
.unwrap();
90+
91+
println!("wasm-strip status: {}", output.status);
92+
io::stdout().write_all(&output.stdout).unwrap();
93+
io::stderr().write_all(&output.stderr).unwrap();
94+
assert!(output.status.success());
95+
}
96+
97+
fn run_wasm_opt(engine_path: impl AsRef<Path>) {
98+
let wasm_opt = which::which("wasm-opt")
99+
.unwrap_or_else(|_| {
100+
PathBuf::from(env::var("OUT_DIR").unwrap())
101+
.join("vendor/wasm-opt")
102+
});
103+
104+
let output = Command::new(wasm_opt)
105+
.arg(engine_path.as_ref())
106+
.arg("-O3")
107+
.arg("--dce")
108+
.arg("-o")
109+
.arg(engine_path.as_ref())
110+
.output()
111+
.unwrap();
112+
113+
println!("wasm-opt status: {}", output.status);
114+
io::stdout().write_all(&output.stdout).unwrap();
115+
io::stderr().write_all(&output.stderr).unwrap();
116+
assert!(output.status.success());
117+
}

0 commit comments

Comments
 (0)