Skip to content

Commit

Permalink
internal: support per pkg link flags for test mode (#421)
Browse files Browse the repository at this point in the history
  • Loading branch information
Young-Flash authored Oct 28, 2024
1 parent dfb67b6 commit 0ae11a1
Show file tree
Hide file tree
Showing 22 changed files with 404 additions and 156 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

38 changes: 3 additions & 35 deletions crates/moon/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ use moonutil::{
cli::UniversalFlags,
common::{
read_module_desc_file_in_dir, BuildPackageFlags, LinkCoreFlags, MooncOpt, OutputFormat,
RunMode, SurfaceTarget, TargetBackend, MOONBITLANG_CORE, MOON_MOD_JSON,
SurfaceTarget, TargetBackend, MOONBITLANG_CORE, MOON_MOD_JSON,
},
mooncakes::{LoginSubcommand, PackageSubcommand, PublishSubcommand, RegisterSubcommand},
};
Expand Down Expand Up @@ -188,46 +188,14 @@ impl BuildFlags {
}
}

pub fn get_compiler_flags(
src_dir: &Path,
build_flags: &BuildFlags,
run_mode: RunMode,
) -> anyhow::Result<MooncOpt> {
pub fn get_compiler_flags(src_dir: &Path, build_flags: &BuildFlags) -> anyhow::Result<MooncOpt> {
// read moon.mod.json
if !moonutil::common::check_moon_mod_exists(src_dir) {
bail!("could not find `{}`", MOON_MOD_JSON);
}
let moon_mod = read_module_desc_file_in_dir(src_dir)?;
let extra_build_opt = moon_mod.compile_flags.unwrap_or_default();
let mut extra_link_opt = moon_mod.link_flags.unwrap_or_default();

#[cfg(unix)]
match run_mode {
// need link-core for build, test and run
RunMode::Build | RunMode::Test | RunMode::Run => {
if build_flags.release && build_flags.target_backend == Some(TargetBackend::Native) {
// check if cc exists in PATH
if let Err(e) = which::which("cc") {
eprintln!(
"error: 'cc' not found in PATH, which is used for native backend release compilation: {}",
e
);
std::process::exit(1);
}
// libmoonbitrun.o should be in the same directory as moonc
let libmoonbitrun_path = which::which("moonc")?.with_file_name("libmoonbitrun.o");

// use "cc" to compile
extra_link_opt.extend_from_slice(&[
"-cc".to_string(),
format!("cc {} -O2 -fwrapv", libmoonbitrun_path.display()),
"-cc-link-flags".to_string(),
"-lm".to_string(),
]);
}
}
_ => {}
}
let extra_link_opt = moon_mod.link_flags.unwrap_or_default();

let output_format = if build_flags.output_wat {
OutputFormat::Wat
Expand Down
11 changes: 9 additions & 2 deletions crates/moon/src/cli/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ fn run_build_internal(

let raw_target_dir = target_dir;
let run_mode = RunMode::Build;
let mut moonc_opt = super::get_compiler_flags(source_dir, &cmd.build_flags, run_mode)?;
let mut moonc_opt = super::get_compiler_flags(source_dir, &cmd.build_flags)?;
moonc_opt.build_opt.deny_warn = cmd.build_flags.deny_warn;
let target_dir = mk_arch_mode_dir(source_dir, target_dir, &moonc_opt, run_mode)?;
let _lock = FileLock::lock(&target_dir)?;
Expand All @@ -140,14 +140,21 @@ fn run_build_internal(
no_parallelize: false,
};

let module = scan_with_pre_build(
let mut module = scan_with_pre_build(
false,
&moonc_opt,
&moonbuild_opt,
&resolved_env,
&dir_sync_result,
)?;

moonutil::common::set_native_backend_link_flags(
run_mode,
cmd.build_flags.release,
cmd.build_flags.target_backend,
&mut module,
);

moonc_opt.build_opt.warn_lists = module
.get_all_packages()
.iter()
Expand Down
2 changes: 1 addition & 1 deletion crates/moon/src/cli/bundle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ fn run_bundle_internal(
)?;

let run_mode = RunMode::Bundle;
let mut moonc_opt = super::get_compiler_flags(source_dir, &cmd.build_flags, run_mode)?;
let mut moonc_opt = super::get_compiler_flags(source_dir, &cmd.build_flags)?;
let sort_input = cmd.build_flags.sort_input;

let raw_target_dir = target_dir.to_path_buf();
Expand Down
2 changes: 1 addition & 1 deletion crates/moon/src/cli/check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ fn run_check_internal(

let raw_target_dir = target_dir;
let run_mode = RunMode::Check;
let mut moonc_opt = get_compiler_flags(source_dir, &cmd.build_flags, run_mode)?;
let mut moonc_opt = get_compiler_flags(source_dir, &cmd.build_flags)?;
moonc_opt.build_opt.deny_warn = cmd.build_flags.deny_warn;
let target_dir = mk_arch_mode_dir(source_dir, target_dir, &moonc_opt, run_mode)?;
let _lock = FileLock::lock(&target_dir)?;
Expand Down
2 changes: 1 addition & 1 deletion crates/moon/src/cli/generate_test_driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ pub fn generate_test_driver(
cmd.build_flags.target_backend = target_backend;

let run_mode = RunMode::Test;
let moonc_opt = super::get_compiler_flags(&source_dir, &cmd.build_flags, run_mode)?;
let moonc_opt = super::get_compiler_flags(&source_dir, &cmd.build_flags)?;

let sort_input = cmd.build_flags.sort_input;
let filter_package = cmd.package.map(|it| it.into_iter().collect());
Expand Down
11 changes: 9 additions & 2 deletions crates/moon/src/cli/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ pub fn run_run_internal(cli: &UniversalFlags, cmd: RunSubcommand) -> anyhow::Res
)?;

let run_mode = RunMode::Run;
let mut moonc_opt = super::get_compiler_flags(&source_dir, &cmd.build_flags, run_mode)?;
let mut moonc_opt = super::get_compiler_flags(&source_dir, &cmd.build_flags)?;

let raw_target_dir = target_dir.to_path_buf();
let target_dir = mk_arch_mode_dir(&source_dir, &target_dir, &moonc_opt, run_mode)?;
Expand Down Expand Up @@ -279,14 +279,21 @@ pub fn run_run_internal(cli: &UniversalFlags, cmd: RunSubcommand) -> anyhow::Res
no_parallelize: false,
};

let module = scan_with_pre_build(
let mut module = scan_with_pre_build(
false,
&moonc_opt,
&moonbuild_opt,
&resolved_env,
&dir_sync_result,
)?;

moonutil::common::set_native_backend_link_flags(
run_mode,
cmd.build_flags.release,
cmd.build_flags.target_backend,
&mut module,
);

moonc_opt.build_opt.warn_lists = module
.get_all_packages()
.iter()
Expand Down
9 changes: 8 additions & 1 deletion crates/moon/src/cli/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ fn run_test_internal(
)?;

let run_mode = RunMode::Test;
let mut moonc_opt = super::get_compiler_flags(source_dir, &cmd.build_flags, run_mode)?;
let mut moonc_opt = super::get_compiler_flags(source_dir, &cmd.build_flags)?;
// 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;
Expand Down Expand Up @@ -305,6 +305,13 @@ fn run_test_internal(
}
}

moonutil::common::set_native_backend_link_flags(
run_mode,
cmd.build_flags.release,
cmd.build_flags.target_backend,
&mut module,
);

moonc_opt.build_opt.warn_lists = module
.get_all_packages()
.iter()
Expand Down
Loading

0 comments on commit 0ae11a1

Please sign in to comment.