Skip to content

Commit

Permalink
Merge pull request #532 from moonbitlang/strip_debug_info
Browse files Browse the repository at this point in the history
internal: add --strip & --no-strip cli confg
  • Loading branch information
Young-Flash authored Dec 19, 2024
2 parents 5a38a2f + 6acec0d commit e628c26
Show file tree
Hide file tree
Showing 14 changed files with 195 additions and 5 deletions.
19 changes: 19 additions & 0 deletions crates/moon/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,14 @@ pub struct BuildFlags {
#[clap(long, conflicts_with = "debug")]
pub release: bool,

/// Enable stripping debug information
#[clap(long, conflicts_with = "no_strip")]
pub strip: bool,

/// Disable stripping debug information
#[clap(long, conflicts_with = "strip")]
pub no_strip: bool,

/// Select output target
#[clap(long, value_delimiter = ',')]
pub target: Option<Vec<SurfaceTarget>>,
Expand Down Expand Up @@ -201,6 +209,16 @@ impl BuildFlags {
(true, true) => panic!("both std and no_std flags are set"),
}
}

pub fn strip(&self) -> bool {
if self.strip {
true
} else if self.no_strip {
false
} else {
!self.debug
}
}
}

pub fn get_compiler_flags(src_dir: &Path, build_flags: &BuildFlags) -> anyhow::Result<MooncOpt> {
Expand Down Expand Up @@ -237,6 +255,7 @@ pub fn get_compiler_flags(src_dir: &Path, build_flags: &BuildFlags) -> anyhow::R

let build_opt = BuildPackageFlags {
debug_flag,
strip_flag: build_flags.strip(),
source_map,
enable_coverage,
deny_warn: false,
Expand Down
7 changes: 7 additions & 0 deletions crates/moon/src/cli/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,13 @@ fn run_test_internal(
// release is 'false' by default, so we will run test at debug mode(to gain more detailed stack trace info), unless `--release` is specified
// however, other command like build, check, run, etc, will run at release mode by default
moonc_opt.build_opt.debug_flag = !cmd.build_flags.release;
moonc_opt.build_opt.strip_flag = if cmd.build_flags.strip {
true
} else if cmd.build_flags.no_strip {
false
} else {
cmd.build_flags.release
};
moonc_opt.link_opt.debug_flag = !cmd.build_flags.release;

let raw_target_dir = target_dir.to_path_buf();
Expand Down
87 changes: 87 additions & 0 deletions crates/moon/tests/test_cases/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8131,3 +8131,90 @@ fn test_moon_install_bin() {
"#]],
);
}

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

check(
get_stdout(&dir, ["build", "--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
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
"#]],
);
check(
get_stdout(&dir, ["build", "--no-strip", "--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 -g
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 -g
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 -g
"#]],
);
check(
get_stdout(&dir, ["build", "--debug", "--strip", "--dry-run"]),
expect![[r#"
moonc build-package ./lib/hello.mbt -o ./target/wasm-gc/debug/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 -O0 -source-map
moonc build-package ./main/main.mbt -o ./target/wasm-gc/debug/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/debug/build/lib/lib.mi:lib -pkg-sources moon_new/main:./main -target wasm-gc -O0 -source-map
moonc link-core $MOON_HOME/lib/core/target/wasm-gc/release/bundle/core.core ./target/wasm-gc/debug/build/lib/lib.core ./target/wasm-gc/debug/build/main/main.core -main moon_new/main -o ./target/wasm-gc/debug/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 -O0 -source-map
"#]],
);
check(
get_stdout(&dir, ["build", "--debug", "--no-strip", "--dry-run"]),
expect![[r#"
moonc build-package ./lib/hello.mbt -o ./target/wasm-gc/debug/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 -g -O0 -source-map
moonc build-package ./main/main.mbt -o ./target/wasm-gc/debug/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/debug/build/lib/lib.mi:lib -pkg-sources moon_new/main:./main -target wasm-gc -g -O0 -source-map
moonc link-core $MOON_HOME/lib/core/target/wasm-gc/release/bundle/core.core ./target/wasm-gc/debug/build/lib/lib.core ./target/wasm-gc/debug/build/main/main.core -main moon_new/main -o ./target/wasm-gc/debug/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 -g -O0 -source-map
"#]],
);
check(
get_stdout(&dir, ["build", "--debug", "--dry-run"]),
expect![[r#"
moonc build-package ./lib/hello.mbt -o ./target/wasm-gc/debug/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 -g -O0 -source-map
moonc build-package ./main/main.mbt -o ./target/wasm-gc/debug/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/debug/build/lib/lib.mi:lib -pkg-sources moon_new/main:./main -target wasm-gc -g -O0 -source-map
moonc link-core $MOON_HOME/lib/core/target/wasm-gc/release/bundle/core.core ./target/wasm-gc/debug/build/lib/lib.core ./target/wasm-gc/debug/build/main/main.core -main moon_new/main -o ./target/wasm-gc/debug/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 -g -O0 -source-map
"#]],
);

check(
get_stdout(&dir, ["test", "--dry-run"]),
expect![[r#"
moon generate-test-driver --source-dir . --target-dir ./target --package moon_new/lib --target wasm-gc --driver-kind internal
moonc build-package ./lib/hello.mbt ./target/wasm-gc/debug/test/lib/__generated_driver_for_internal_test.mbt -o ./target/wasm-gc/debug/test/lib/lib.internal_test.core -pkg moon_new/lib -is-main -std-path $MOON_HOME/lib/core/target/wasm-gc/release/bundle -pkg-sources moon_new/lib:./lib -target wasm-gc -g -O0 -no-mi
moonc link-core $MOON_HOME/lib/core/target/wasm-gc/release/bundle/core.core ./target/wasm-gc/debug/test/lib/lib.internal_test.core -main moon_new/lib -o ./target/wasm-gc/debug/test/lib/lib.internal_test.wasm -test-mode -pkg-config-path ./lib/moon.pkg.json -pkg-sources moon_new/lib:./lib -pkg-sources moonbitlang/core:$MOON_HOME/lib/core -exported_functions moonbit_test_driver_internal_execute,moonbit_test_driver_finish -target wasm-gc -g -O0
"#]],
);
check(
get_stdout(&dir, ["test", "--debug", "--dry-run"]),
expect![[r#"
moon generate-test-driver --source-dir . --target-dir ./target --package moon_new/lib --target wasm-gc --driver-kind internal
moonc build-package ./lib/hello.mbt ./target/wasm-gc/debug/test/lib/__generated_driver_for_internal_test.mbt -o ./target/wasm-gc/debug/test/lib/lib.internal_test.core -pkg moon_new/lib -is-main -std-path $MOON_HOME/lib/core/target/wasm-gc/release/bundle -pkg-sources moon_new/lib:./lib -target wasm-gc -g -O0 -source-map -no-mi
moonc link-core $MOON_HOME/lib/core/target/wasm-gc/release/bundle/core.core ./target/wasm-gc/debug/test/lib/lib.internal_test.core -main moon_new/lib -o ./target/wasm-gc/debug/test/lib/lib.internal_test.wasm -test-mode -pkg-config-path ./lib/moon.pkg.json -pkg-sources moon_new/lib:./lib -pkg-sources moonbitlang/core:$MOON_HOME/lib/core -exported_functions moonbit_test_driver_internal_execute,moonbit_test_driver_finish -target wasm-gc -g -O0 -source-map
"#]],
);
check(
get_stdout(&dir, ["test", "--release", "--dry-run"]),
expect![[r#"
moon generate-test-driver --source-dir . --target-dir ./target --package moon_new/lib --target wasm-gc --driver-kind internal --release
moonc build-package ./lib/hello.mbt ./target/wasm-gc/release/test/lib/__generated_driver_for_internal_test.mbt -o ./target/wasm-gc/release/test/lib/lib.internal_test.core -pkg moon_new/lib -is-main -std-path $MOON_HOME/lib/core/target/wasm-gc/release/bundle -pkg-sources moon_new/lib:./lib -target wasm-gc -no-mi
moonc link-core $MOON_HOME/lib/core/target/wasm-gc/release/bundle/core.core ./target/wasm-gc/release/test/lib/lib.internal_test.core -main moon_new/lib -o ./target/wasm-gc/release/test/lib/lib.internal_test.wasm -test-mode -pkg-config-path ./lib/moon.pkg.json -pkg-sources moon_new/lib:./lib -pkg-sources moonbitlang/core:$MOON_HOME/lib/core -exported_functions moonbit_test_driver_internal_execute,moonbit_test_driver_finish -target wasm-gc
"#]],
);
check(
get_stdout(&dir, ["test", "--release", "--no-strip", "--dry-run"]),
expect![[r#"
moon generate-test-driver --source-dir . --target-dir ./target --package moon_new/lib --target wasm-gc --driver-kind internal --release
moonc build-package ./lib/hello.mbt ./target/wasm-gc/release/test/lib/__generated_driver_for_internal_test.mbt -o ./target/wasm-gc/release/test/lib/lib.internal_test.core -pkg moon_new/lib -is-main -std-path $MOON_HOME/lib/core/target/wasm-gc/release/bundle -pkg-sources moon_new/lib:./lib -target wasm-gc -g -no-mi
moonc link-core $MOON_HOME/lib/core/target/wasm-gc/release/bundle/core.core ./target/wasm-gc/release/test/lib/lib.internal_test.core -main moon_new/lib -o ./target/wasm-gc/release/test/lib/lib.internal_test.wasm -test-mode -pkg-config-path ./lib/moon.pkg.json -pkg-sources moon_new/lib:./lib -pkg-sources moonbitlang/core:$MOON_HOME/lib/core -exported_functions moonbit_test_driver_internal_execute,moonbit_test_driver_finish -target wasm-gc -g
"#]],
);
check(
get_stdout(&dir, ["test", "--debug", "--no-strip", "--dry-run"]),
expect![[r#"
moon generate-test-driver --source-dir . --target-dir ./target --package moon_new/lib --target wasm-gc --driver-kind internal
moonc build-package ./lib/hello.mbt ./target/wasm-gc/debug/test/lib/__generated_driver_for_internal_test.mbt -o ./target/wasm-gc/debug/test/lib/lib.internal_test.core -pkg moon_new/lib -is-main -std-path $MOON_HOME/lib/core/target/wasm-gc/release/bundle -pkg-sources moon_new/lib:./lib -target wasm-gc -g -O0 -source-map -no-mi
moonc link-core $MOON_HOME/lib/core/target/wasm-gc/release/bundle/core.core ./target/wasm-gc/debug/test/lib/lib.internal_test.core -main moon_new/lib -o ./target/wasm-gc/debug/test/lib/lib.internal_test.wasm -test-mode -pkg-config-path ./lib/moon.pkg.json -pkg-sources moon_new/lib:./lib -pkg-sources moonbitlang/core:$MOON_HOME/lib/core -exported_functions moonbit_test_driver_internal_execute,moonbit_test_driver_finish -target wasm-gc -g -O0 -source-map
"#]],
);
}
4 changes: 4 additions & 0 deletions crates/moon/tests/test_cases/strip_debug.in/lib/hello.mbt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
pub fn hello() -> String {
"Hello, world!"
}

3 changes: 3 additions & 0 deletions crates/moon/tests/test_cases/strip_debug.in/lib/moon.pkg.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"import": {}
}
3 changes: 3 additions & 0 deletions crates/moon/tests/test_cases/strip_debug.in/main/main.mbt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
fn main {
println(@lib.hello())
}
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/strip_debug.in/moon.mod.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"name": "moon_new"
}
20 changes: 18 additions & 2 deletions crates/moonbuild/src/gen/gen_build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,11 @@ pub fn gen_build_command(

let mut build = Build::new(loc, ins, outs);

let (debug_flag, strip_flag) = (
moonc_opt.build_opt.debug_flag,
moonc_opt.build_opt.strip_flag,
);

let command = CommandBuilder::new("moonc")
.arg("build-package")
.args_with_cond(moonc_opt.render, vec!["-error-format", "json"])
Expand Down Expand Up @@ -281,7 +286,10 @@ pub fn gen_build_command(
&item.package_full_name, &item.package_source_dir
))
.args(["-target", moonc_opt.build_opt.target_backend.to_flag()])
.args_with_cond(moonc_opt.build_opt.debug_flag, vec!["-g", "-O0"])
.args_with_cond(debug_flag && !strip_flag, vec!["-g", "-O0"])
.arg_with_cond(debug_flag && strip_flag, "-O0")
.arg_with_cond(!debug_flag && !strip_flag, "-g")
// .arg_with_cond(!debug_flag && strip_flag, "")
.arg_with_cond(moonc_opt.link_opt.source_map, "-source-map")
.arg_with_cond(enable_coverage, "-enable-coverage")
.arg_with_cond(self_coverage, "-coverage-package-override=@self")
Expand Down Expand Up @@ -341,6 +349,11 @@ pub fn gen_link_command(
let native_cc_flags = item.native_cc_flags(moonc_opt.link_opt.target_backend);
let native_cc_link_flags = item.native_cc_link_flags(moonc_opt.link_opt.target_backend);

let (debug_flag, strip_flag) = (
moonc_opt.build_opt.debug_flag,
moonc_opt.build_opt.strip_flag,
);

let command = CommandBuilder::new("moonc")
.arg("link-core")
.arg_with_cond(
Expand Down Expand Up @@ -374,7 +387,10 @@ pub fn gen_link_command(
],
)
.args(["-target", moonc_opt.link_opt.target_backend.to_flag()])
.args_with_cond(moonc_opt.link_opt.debug_flag, vec!["-g", "-O0"])
.args_with_cond(debug_flag && !strip_flag, vec!["-g", "-O0"])
.arg_with_cond(debug_flag && strip_flag, "-O0")
.arg_with_cond(!debug_flag && !strip_flag, "-g")
// .arg_with_cond(!debug_flag && strip_flag, "")
.arg_with_cond(moonc_opt.link_opt.source_map, "-source-map")
.lazy_args_with_cond(exports.is_some(), || {
let es = exports.unwrap();
Expand Down
10 changes: 9 additions & 1 deletion crates/moonbuild/src/gen/gen_bundle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,11 @@ pub fn gen_build_command(

let mut build = Build::new(loc, ins, outs);

let (debug_flag, strip_flag) = (
moonc_opt.build_opt.debug_flag,
moonc_opt.build_opt.strip_flag,
);

let command = CommandBuilder::new("moonc")
.arg("build-package")
.args_with_cond(moonc_opt.render, vec!["-error-format", "json"])
Expand All @@ -228,7 +233,10 @@ pub fn gen_build_command(
&item.package_full_name, &item.package_source_dir
))
.args(["-target", moonc_opt.build_opt.target_backend.to_flag()])
.args_with_cond(moonc_opt.build_opt.debug_flag, vec!["-g", "-O0"])
.args_with_cond(debug_flag && !strip_flag, vec!["-g", "-O0"])
.arg_with_cond(debug_flag && strip_flag, "-O0")
.arg_with_cond(!debug_flag && !strip_flag, "-g")
// .arg_with_cond(!debug_flag && strip_flag, "")
.arg_with_cond(moonc_opt.link_opt.source_map, "-source-map")
.args(moonc_opt.extra_build_opt.iter())
.build();
Expand Down
20 changes: 18 additions & 2 deletions crates/moonbuild/src/gen/gen_runtest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -915,6 +915,11 @@ pub fn gen_runtest_build_command(

let mut build = Build::new(loc, ins, outs);

let (debug_flag, strip_flag) = (
moonc_opt.build_opt.debug_flag,
moonc_opt.build_opt.strip_flag,
);

let command = CommandBuilder::new("moonc")
.arg("build-package")
.args_with_cond(moonc_opt.render, vec!["-error-format", "json"])
Expand Down Expand Up @@ -946,7 +951,10 @@ pub fn gen_runtest_build_command(
&item.package_full_name, &item.package_source_dir
))
.args(["-target", moonc_opt.build_opt.target_backend.to_flag()])
.args_with_cond(moonc_opt.build_opt.debug_flag, vec!["-g", "-O0"])
.args_with_cond(debug_flag && !strip_flag, vec!["-g", "-O0"])
.arg_with_cond(debug_flag && strip_flag, "-O0")
.arg_with_cond(!debug_flag && !strip_flag, "-g")
// .arg_with_cond(!debug_flag && strip_flag, "")
.arg_with_cond(moonc_opt.link_opt.source_map, "-source-map")
// Coverage arg
.args(coverage_args.iter())
Expand Down Expand Up @@ -1012,6 +1020,11 @@ pub fn gen_runtest_link_command(
let native_cc_flags = item.native_cc_flags(moonc_opt.link_opt.target_backend);
let native_cc_link_flags = item.native_cc_link_flags(moonc_opt.link_opt.target_backend);

let (debug_flag, strip_flag) = (
moonc_opt.build_opt.debug_flag,
moonc_opt.build_opt.strip_flag,
);

let command = CommandBuilder::new("moonc")
.arg("link-core")
.arg_with_cond(
Expand Down Expand Up @@ -1054,7 +1067,10 @@ pub fn gen_runtest_link_command(
["-js-format", "cjs", "-no-dts"],
)
.args(["-target", moonc_opt.link_opt.target_backend.to_flag()])
.args_with_cond(moonc_opt.link_opt.debug_flag, vec!["-g", "-O0"])
.args_with_cond(debug_flag && !strip_flag, vec!["-g", "-O0"])
.arg_with_cond(debug_flag && strip_flag, "-O0")
.arg_with_cond(!debug_flag && !strip_flag, "-g")
// .arg_with_cond(!debug_flag && strip_flag, "")
.arg_with_cond(moonc_opt.link_opt.source_map, "-source-map")
.lazy_args_with_cond(native_cc.is_some(), || {
vec!["-cc".to_string(), native_cc.unwrap().to_string()]
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 @@ -308,6 +308,7 @@ impl TargetBackend {
#[derive(Debug, Clone, Default)]
pub struct BuildPackageFlags {
pub debug_flag: bool,
pub strip_flag: bool,
pub source_map: bool,
pub enable_coverage: bool,
// treat all warnings as errors
Expand All @@ -321,6 +322,7 @@ impl BuildPackageFlags {
pub fn new() -> Self {
Self {
debug_flag: false,
strip_flag: true,
source_map: false,
enable_coverage: false,
deny_warn: false,
Expand Down
Loading

0 comments on commit e628c26

Please sign in to comment.