Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

internal: support --enable-value-tracing for build-package #534

Merged
merged 2 commits into from
Dec 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions crates/moon/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,10 @@ pub struct BuildFlags {
/// Alert list config
#[clap(long, allow_hyphen_values = true)]
pub alert_list: Option<String>,

/// Enable value tracing
#[clap(long, hide = true)]
pub enable_value_tracing: bool,
}

impl BuildFlags {
Expand Down Expand Up @@ -262,6 +266,7 @@ pub fn get_compiler_flags(src_dir: &Path, build_flags: &BuildFlags) -> anyhow::R
target_backend,
warn_list: build_flags.warn_list.clone(),
alert_list: build_flags.alert_list.clone(),
enable_value_tracing: build_flags.enable_value_tracing,
};

let link_opt = LinkCoreFlags {
Expand Down
38 changes: 22 additions & 16 deletions crates/moon/src/cli/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,22 +110,25 @@ fn run_single_mbt_file(cli: &UniversalFlags, cmd: RunSubcommand) -> anyhow::Resu
output_artifact_path.join(format!("{}.{}", file_name, target_backend.to_extension()));

let pkg_name = "moon/run/single";
let build_package_command = [
"build-package",
&mbt_file_path.display().to_string(),
"-o",
output_core_path,
"-std-path",
core_bundle_path.to_str().unwrap(),
"-is-main",
"-pkg",
pkg_name,
"-g",
"-O0",
"-source-map",
"-target",
target_backend.to_flag(),
let mut build_package_command = vec![
"build-package".to_string(),
mbt_file_path.display().to_string(),
"-o".to_string(),
output_core_path.to_string(),
"-std-path".to_string(),
core_bundle_path.to_str().unwrap().to_string(),
"-is-main".to_string(),
"-pkg".to_string(),
pkg_name.to_string(),
"-g".to_string(),
"-O0".to_string(),
"-source-map".to_string(),
"-target".to_string(),
target_backend.to_flag().to_string(),
];
if cmd.build_flags.enable_value_tracing {
build_package_command.push("-enable-value-tracing".to_string());
}
let link_core_command = [
"link-core",
&moonutil::moon_dir::core_core(target_backend)
Expand Down Expand Up @@ -167,7 +170,7 @@ fn run_single_mbt_file(cli: &UniversalFlags, cmd: RunSubcommand) -> anyhow::Resu
}

let moonc_build_package = std::process::Command::new("moonc")
.args(build_package_command)
.args(&build_package_command)
.stdout(std::process::Stdio::inherit())
.stderr(std::process::Stdio::inherit())
.spawn()?
Expand Down Expand Up @@ -295,6 +298,9 @@ pub fn run_run_internal(cli: &UniversalFlags, cmd: RunSubcommand) -> anyhow::Res
&dir_sync_result,
)?;

let pkg = module.get_package_by_path_mut(&package).unwrap();
pkg.enable_value_tracing = cmd.build_flags.enable_value_tracing;

moonutil::common::set_native_backend_link_flags(
run_mode,
cmd.build_flags.release,
Expand Down
68 changes: 68 additions & 0 deletions crates/moon/tests/test_cases/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8218,3 +8218,71 @@ fn test_strip_debug() {
"#]],
);
}

#[test]
fn test_tracing_value() {
let dir = TestDir::new("tracing_value.in");

// main.mbt in package
check(
get_stdout(
&dir,
[
"run",
"./main/main.mbt",
"--enable-value-tracing",
"--dry-run",
],
),
expect![[r#"
moonc build-package ./lib/hello.mbt -o ./target/wasm-gc/release/build/lib/lib.core -pkg moon_new/lib -std-path $MOON_HOME/lib/core/target/wasm-gc/release/bundle -pkg-sources moon_new/lib:./lib -target wasm-gc
moonc build-package ./main/main.mbt -o ./target/wasm-gc/release/build/main/main.core -pkg moon_new/main -is-main -std-path $MOON_HOME/lib/core/target/wasm-gc/release/bundle -i ./target/wasm-gc/release/build/lib/lib.mi:lib -pkg-sources moon_new/main:./main -target wasm-gc -enable-value-tracing
moonc link-core $MOON_HOME/lib/core/target/wasm-gc/release/bundle/core.core ./target/wasm-gc/release/build/lib/lib.core ./target/wasm-gc/release/build/main/main.core -main moon_new/main -o ./target/wasm-gc/release/build/main/main.wasm -pkg-config-path ./main/moon.pkg.json -pkg-sources moon_new/lib:./lib -pkg-sources moon_new/main:./main -pkg-sources moonbitlang/core:$MOON_HOME/lib/core -target wasm-gc
moonrun ./target/wasm-gc/release/build/main/main.wasm
"#]],
);
check(
get_stdout(&dir, ["run", "./main/main.mbt", "--enable-value-tracing"]),
expect![[r#"
Hello, world!
######MOONBIT_VALUE_TRACING_START######
{"name":"a","value":"1","line":"3","start_column":"7","end_column":"8"}
######MOONBIT_VALUE_TRACING_END######
######MOONBIT_VALUE_TRACING_START######
{"name":"b","value":"2","line":"4","start_column":"7","end_column":"8"}
######MOONBIT_VALUE_TRACING_END######
######MOONBIT_VALUE_TRACING_START######
{"name":"c","value":"3","line":"5","start_column":"7","end_column":"8"}
######MOONBIT_VALUE_TRACING_END######
3
"#]],
);

// single file
check(
get_stdout(
&dir,
["run", "./main.mbt", "--enable-value-tracing", "--dry-run"],
),
expect![[r#"
moonc build-package $ROOT/main.mbt -o $ROOT/target/main.core -std-path $MOON_HOME/lib/core/target/wasm-gc/release/bundle -is-main -pkg moon/run/single -g -O0 -source-map -target wasm-gc -enable-value-tracing
moonc link-core $MOON_HOME/lib/core/target/wasm-gc/release/bundle/core.core $ROOT/target/main.core -o $ROOT/target/main.wasm -pkg-sources moon/run/single:$ROOT -pkg-sources moonbitlang/core:$MOON_HOME/lib/core -g -O0 -source-map -target wasm-gc
moonrun $ROOT/target/main.wasm
"#]],
);
check(
get_stdout(&dir, ["run", "./main.mbt", "--enable-value-tracing"]),
expect![[r#######"
######MOONBIT_VALUE_TRACING_START######
{"name":"a","value":"1","line":"2","start_column":"7","end_column":"8"}
######MOONBIT_VALUE_TRACING_END######
######MOONBIT_VALUE_TRACING_START######
{"name":"b","value":"2","line":"3","start_column":"7","end_column":"8"}
######MOONBIT_VALUE_TRACING_END######
######MOONBIT_VALUE_TRACING_START######
{"name":"c","value":"3","line":"4","start_column":"7","end_column":"8"}
######MOONBIT_VALUE_TRACING_END######
3
"#######]],
);
}
4 changes: 4 additions & 0 deletions crates/moon/tests/test_cases/tracing_value.in/lib/hello.mbt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
pub fn hello() -> String {
"Hello, world!"
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"import": {}
}
6 changes: 6 additions & 0 deletions crates/moon/tests/test_cases/tracing_value.in/main.mbt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
fn main {
let a = 1
let b = 2
let c = a + b
println(c)
}
7 changes: 7 additions & 0 deletions crates/moon/tests/test_cases/tracing_value.in/main/main.mbt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
fn main {
println(@lib.hello())
let a = 1
let b = 2
let c = a + b
println(c)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"is-main": true,
"import": {
"moon_new/lib": ""
}
}
3 changes: 3 additions & 0 deletions crates/moon/tests/test_cases/tracing_value.in/moon.mod.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"name": "moon_new"
}
3 changes: 3 additions & 0 deletions crates/moonbuild/src/gen/gen_build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ pub struct BuildDepItem {
pub alert_list: Option<String>,
pub is_main: bool,
pub is_third_party: bool,
pub enable_value_tracing: bool,
}

type BuildLinkDepItem = moonutil::package::LinkDepItem;
Expand Down Expand Up @@ -118,6 +119,7 @@ pub fn gen_build_build_item(
alert_list: pkg.alert_list.clone(),
is_main: pkg.is_main,
is_third_party: pkg.is_third_party,
enable_value_tracing: pkg.enable_value_tracing,
})
}

Expand Down Expand Up @@ -294,6 +296,7 @@ pub fn gen_build_command(
.arg_with_cond(enable_coverage, "-enable-coverage")
.arg_with_cond(self_coverage, "-coverage-package-override=@self")
.args(moonc_opt.extra_build_opt.iter())
.arg_with_cond(item.enable_value_tracing, "-enable-value-tracing")
.build();
log::debug!("Command: {}", command);
build.cmdline = Some(command);
Expand Down
2 changes: 2 additions & 0 deletions crates/moonutil/src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,7 @@ pub struct BuildPackageFlags {
pub target_backend: TargetBackend,
pub warn_list: Option<String>,
pub alert_list: Option<String>,
pub enable_value_tracing: bool,
}

impl BuildPackageFlags {
Expand All @@ -329,6 +330,7 @@ impl BuildPackageFlags {
target_backend: TargetBackend::default(),
warn_list: None,
alert_list: None,
enable_value_tracing: false,
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions crates/moonutil/src/package.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ pub struct Package {
pub bin_name: Option<String>,

pub bin_target: TargetBackend,

pub enable_value_tracing: bool,
}

impl Package {
Expand Down
1 change: 1 addition & 0 deletions crates/moonutil/src/scan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,7 @@ fn scan_one_package(
.filter(|_| pkg.is_main && !is_third_party),
bin_name: pkg.bin_name,
bin_target: pkg.bin_target,
enable_value_tracing: false,
};
if doc_mode {
// -o <folder>
Expand Down
Loading