Skip to content

Commit

Permalink
internal: add --strip & --no-strip cli confg
Browse files Browse the repository at this point in the history
  • Loading branch information
Young-Flash committed Dec 19, 2024
1 parent 5a38a2f commit c7ed7cd
Show file tree
Hide file tree
Showing 8 changed files with 89 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 == false
}
}
}

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 {
moonc_opt.build_opt.debug_flag == false
};
moonc_opt.link_opt.debug_flag = !cmd.build_flags.release;

let raw_target_dir = target_dir.to_path_buf();
Expand Down
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
8 changes: 8 additions & 0 deletions docs/manual-zh/src/commands.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,8 @@ Build the current package
* `--nostd` — Disable the standard library
* `-g`, `--debug` — Emit debug information
* `--release` — Compile in release mode
* `--strip` — Enable stripping debug information
* `--no-strip` — Disable stripping debug information
* `--target <TARGET>` — Select output target

Possible values: `wasm`, `wasm-gc`, `js`, `native`, `all`
Expand Down Expand Up @@ -131,6 +133,8 @@ Check the current package, but don't build object files
* `--nostd` — Disable the standard library
* `-g`, `--debug` — Emit debug information
* `--release` — Compile in release mode
* `--strip` — Enable stripping debug information
* `--no-strip` — Disable stripping debug information
* `--target <TARGET>` — Select output target

Possible values: `wasm`, `wasm-gc`, `js`, `native`, `all`
Expand Down Expand Up @@ -168,6 +172,8 @@ Run a main package
* `--nostd` — Disable the standard library
* `-g`, `--debug` — Emit debug information
* `--release` — Compile in release mode
* `--strip` — Enable stripping debug information
* `--no-strip` — Disable stripping debug information
* `--target <TARGET>` — Select output target

Possible values: `wasm`, `wasm-gc`, `js`, `native`, `all`
Expand Down Expand Up @@ -197,6 +203,8 @@ Test the current package
* `--nostd` — Disable the standard library
* `-g`, `--debug` — Emit debug information
* `--release` — Compile in release mode
* `--strip` — Enable stripping debug information
* `--no-strip` — Disable stripping debug information
* `--target <TARGET>` — Select output target

Possible values: `wasm`, `wasm-gc`, `js`, `native`, `all`
Expand Down
8 changes: 8 additions & 0 deletions docs/manual/src/commands.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,8 @@ Build the current package
* `--nostd` — Disable the standard library
* `-g`, `--debug` — Emit debug information
* `--release` — Compile in release mode
* `--strip` — Enable stripping debug information
* `--no-strip` — Disable stripping debug information
* `--target <TARGET>` — Select output target

Possible values: `wasm`, `wasm-gc`, `js`, `native`, `all`
Expand Down Expand Up @@ -131,6 +133,8 @@ Check the current package, but don't build object files
* `--nostd` — Disable the standard library
* `-g`, `--debug` — Emit debug information
* `--release` — Compile in release mode
* `--strip` — Enable stripping debug information
* `--no-strip` — Disable stripping debug information
* `--target <TARGET>` — Select output target

Possible values: `wasm`, `wasm-gc`, `js`, `native`, `all`
Expand Down Expand Up @@ -168,6 +172,8 @@ Run a main package
* `--nostd` — Disable the standard library
* `-g`, `--debug` — Emit debug information
* `--release` — Compile in release mode
* `--strip` — Enable stripping debug information
* `--no-strip` — Disable stripping debug information
* `--target <TARGET>` — Select output target

Possible values: `wasm`, `wasm-gc`, `js`, `native`, `all`
Expand Down Expand Up @@ -197,6 +203,8 @@ Test the current package
* `--nostd` — Disable the standard library
* `-g`, `--debug` — Emit debug information
* `--release` — Compile in release mode
* `--strip` — Enable stripping debug information
* `--no-strip` — Disable stripping debug information
* `--target <TARGET>` — Select output target

Possible values: `wasm`, `wasm-gc`, `js`, `native`, `all`
Expand Down

0 comments on commit c7ed7cd

Please sign in to comment.