From 6abe2d918af5634c5b3c80f6979fc33baf893001 Mon Sep 17 00:00:00 2001 From: Young-Flash Date: Thu, 19 Dec 2024 11:24:21 +0800 Subject: [PATCH] feat: support set backend in mod.json --- crates/moon/src/cli.rs | 2 +- .../backend_in_mod_json.in/README.md | 3 ++ .../backend_in_mod_json.in/lib/hello.mbt | 3 ++ .../backend_in_mod_json.in/lib/moon.pkg.json | 1 + .../backend_in_mod_json.in/main/main.mbt | 4 ++ .../backend_in_mod_json.in/main/moon.pkg.json | 4 ++ .../backend_in_mod_json.in/moon.mod.json | 4 ++ crates/moon/tests/test_cases/mod.rs | 39 +++++++++++++++++++ crates/moonbuild/src/bench.rs | 2 + crates/moonbuild/src/new.rs | 2 + crates/moonbuild/template/mod.schema.json | 7 ++++ crates/mooncake/src/pkg/remove.rs | 2 +- crates/mooncake/src/resolver/mvs.rs | 1 + crates/moonutil/src/common.rs | 2 + crates/moonutil/src/module.rs | 33 ++++++++++++++-- .../manual-zh/src/source/mod_json_schema.html | 7 ++++ docs/manual/src/source/mod_json_schema.html | 7 ++++ 17 files changed, 117 insertions(+), 6 deletions(-) create mode 100644 crates/moon/tests/test_cases/backend_in_mod_json.in/README.md create mode 100644 crates/moon/tests/test_cases/backend_in_mod_json.in/lib/hello.mbt create mode 100644 crates/moon/tests/test_cases/backend_in_mod_json.in/lib/moon.pkg.json create mode 100644 crates/moon/tests/test_cases/backend_in_mod_json.in/main/main.mbt create mode 100644 crates/moon/tests/test_cases/backend_in_mod_json.in/main/moon.pkg.json create mode 100644 crates/moon/tests/test_cases/backend_in_mod_json.in/moon.mod.json diff --git a/crates/moon/src/cli.rs b/crates/moon/src/cli.rs index 36f160e7..78c4ad0d 100644 --- a/crates/moon/src/cli.rs +++ b/crates/moon/src/cli.rs @@ -218,7 +218,7 @@ pub fn get_compiler_flags(src_dir: &Path, build_flags: &BuildFlags) -> anyhow::R OutputFormat::Wasm }; - let target_backend = build_flags.target_backend.unwrap_or_default(); + let target_backend = build_flags.target_backend.unwrap_or(moon_mod.backend); if target_backend == TargetBackend::Js && output_format == OutputFormat::Wat { bail!("--output-wat is not supported for --target js"); diff --git a/crates/moon/tests/test_cases/backend_in_mod_json.in/README.md b/crates/moon/tests/test_cases/backend_in_mod_json.in/README.md new file mode 100644 index 00000000..38ca2461 --- /dev/null +++ b/crates/moon/tests/test_cases/backend_in_mod_json.in/README.md @@ -0,0 +1,3 @@ +## MoonBit Unicode + +We can use emoji in MoonBit now :) diff --git a/crates/moon/tests/test_cases/backend_in_mod_json.in/lib/hello.mbt b/crates/moon/tests/test_cases/backend_in_mod_json.in/lib/hello.mbt new file mode 100644 index 00000000..427f5685 --- /dev/null +++ b/crates/moon/tests/test_cases/backend_in_mod_json.in/lib/hello.mbt @@ -0,0 +1,3 @@ +test { + +} diff --git a/crates/moon/tests/test_cases/backend_in_mod_json.in/lib/moon.pkg.json b/crates/moon/tests/test_cases/backend_in_mod_json.in/lib/moon.pkg.json new file mode 100644 index 00000000..9e26dfee --- /dev/null +++ b/crates/moon/tests/test_cases/backend_in_mod_json.in/lib/moon.pkg.json @@ -0,0 +1 @@ +{} \ No newline at end of file diff --git a/crates/moon/tests/test_cases/backend_in_mod_json.in/main/main.mbt b/crates/moon/tests/test_cases/backend_in_mod_json.in/main/main.mbt new file mode 100644 index 00000000..0a3643c8 --- /dev/null +++ b/crates/moon/tests/test_cases/backend_in_mod_json.in/main/main.mbt @@ -0,0 +1,4 @@ +fn main { + let 🤣 = 3 + println(🤣) +} diff --git a/crates/moon/tests/test_cases/backend_in_mod_json.in/main/moon.pkg.json b/crates/moon/tests/test_cases/backend_in_mod_json.in/main/moon.pkg.json new file mode 100644 index 00000000..d48c4d24 --- /dev/null +++ b/crates/moon/tests/test_cases/backend_in_mod_json.in/main/moon.pkg.json @@ -0,0 +1,4 @@ +{ + "is-main": true, + "import": {} +} \ No newline at end of file diff --git a/crates/moon/tests/test_cases/backend_in_mod_json.in/moon.mod.json b/crates/moon/tests/test_cases/backend_in_mod_json.in/moon.mod.json new file mode 100644 index 00000000..f4def3ef --- /dev/null +++ b/crates/moon/tests/test_cases/backend_in_mod_json.in/moon.mod.json @@ -0,0 +1,4 @@ +{ + "name": "unicode_demo", + "backend": "js" +} diff --git a/crates/moon/tests/test_cases/mod.rs b/crates/moon/tests/test_cases/mod.rs index 77372c34..51f8fb45 100644 --- a/crates/moon/tests/test_cases/mod.rs +++ b/crates/moon/tests/test_cases/mod.rs @@ -8131,3 +8131,42 @@ fn test_moon_install_bin() { "#]], ); } + +#[test] +fn test_set_backend_in_mod_json() { + let dir = TestDir::new("backend_in_mod_json.in"); + + check( + get_stdout(&dir, ["check", "--dry-run", "--sort-input"]), + expect![[r#" + moonc check ./main/main.mbt -o ./target/js/release/check/main/main.mi -pkg unicode_demo/main -is-main -std-path $MOON_HOME/lib/core/target/js/release/bundle -pkg-sources unicode_demo/main:./main -target js + moonc check ./lib/hello.mbt -o ./target/js/release/check/lib/lib.mi -pkg unicode_demo/lib -std-path $MOON_HOME/lib/core/target/js/release/bundle -pkg-sources unicode_demo/lib:./lib -target js + "#]], + ); + + check( + get_stdout(&dir, ["build", "--dry-run", "--sort-input"]), + expect![[r#" + moonc build-package ./main/main.mbt -o ./target/js/release/build/main/main.core -pkg unicode_demo/main -is-main -std-path $MOON_HOME/lib/core/target/js/release/bundle -pkg-sources unicode_demo/main:./main -target js + moonc link-core $MOON_HOME/lib/core/target/js/release/bundle/core.core ./target/js/release/build/main/main.core -main unicode_demo/main -o ./target/js/release/build/main/main.js -pkg-config-path ./main/moon.pkg.json -pkg-sources unicode_demo/main:./main -pkg-sources moonbitlang/core:$MOON_HOME/lib/core -target js + "#]], + ); + + check( + get_stdout(&dir, ["run", "main", "--dry-run", "--sort-input"]), + expect![[r#" + moonc build-package ./main/main.mbt -o ./target/js/release/build/main/main.core -pkg unicode_demo/main -is-main -std-path $MOON_HOME/lib/core/target/js/release/bundle -pkg-sources unicode_demo/main:./main -target js + moonc link-core $MOON_HOME/lib/core/target/js/release/bundle/core.core ./target/js/release/build/main/main.core -main unicode_demo/main -o ./target/js/release/build/main/main.js -pkg-config-path ./main/moon.pkg.json -pkg-sources unicode_demo/main:./main -pkg-sources moonbitlang/core:$MOON_HOME/lib/core -target js + node ./target/js/release/build/main/main.js + "#]], + ); + + check( + get_stdout(&dir, ["test", "--dry-run", "--sort-input"]), + expect![[r#" + moon generate-test-driver --source-dir . --target-dir ./target --package unicode_demo/lib --sort-input --target js --driver-kind internal + moonc build-package ./lib/hello.mbt ./target/js/debug/test/lib/__generated_driver_for_internal_test.mbt -o ./target/js/debug/test/lib/lib.internal_test.core -pkg unicode_demo/lib -is-main -std-path $MOON_HOME/lib/core/target/js/release/bundle -pkg-sources unicode_demo/lib:./lib -target js -g -O0 -no-mi + moonc link-core $MOON_HOME/lib/core/target/js/release/bundle/core.core ./target/js/debug/test/lib/lib.internal_test.core -main unicode_demo/lib -o ./target/js/debug/test/lib/lib.internal_test.js -test-mode -pkg-config-path ./lib/moon.pkg.json -pkg-sources unicode_demo/lib:./lib -pkg-sources moonbitlang/core:$MOON_HOME/lib/core -exported_functions moonbit_test_driver_internal_execute,moonbit_test_driver_finish -js-format cjs -no-dts -target js -g -O0 + "#]], + ); +} diff --git a/crates/moonbuild/src/bench.rs b/crates/moonbuild/src/bench.rs index b660e445..095ac27d 100644 --- a/crates/moonbuild/src/bench.rs +++ b/crates/moonbuild/src/bench.rs @@ -148,6 +148,8 @@ pub fn write(config: &Config, base_dir: &Path) { include: None, exclude: None, + + backend: None, }; moonutil::common::write_module_json_to_file(&module, base_dir).unwrap(); fs::create_dir_all(base_dir.join("main")).unwrap(); diff --git a/crates/moonbuild/src/new.rs b/crates/moonbuild/src/new.rs index 392010e4..fd42e686 100644 --- a/crates/moonbuild/src/new.rs +++ b/crates/moonbuild/src/new.rs @@ -174,6 +174,8 @@ fn common( include: None, exclude: None, + + backend: None, }; moonutil::common::write_module_json_to_file(&m, target_dir) .context(format!("failed to write `{}`", MOON_MOD_JSON))?; diff --git a/crates/moonbuild/template/mod.schema.json b/crates/moonbuild/template/mod.schema.json index 5c146acc..7d7ad7d4 100644 --- a/crates/moonbuild/template/mod.schema.json +++ b/crates/moonbuild/template/mod.schema.json @@ -14,6 +14,13 @@ "null" ] }, + "backend": { + "description": "Restrict backend to build the module.", + "type": [ + "string", + "null" + ] + }, "bin-deps": { "description": "third-party binary dependencies of the module", "type": [ diff --git a/crates/mooncake/src/pkg/remove.rs b/crates/mooncake/src/pkg/remove.rs index 28775203..991a002d 100644 --- a/crates/mooncake/src/pkg/remove.rs +++ b/crates/mooncake/src/pkg/remove.rs @@ -51,7 +51,7 @@ pub fn remove( pkgname, ) } - let m = Rc::new(m); + let m: Rc = Rc::new(m); let ms = ModuleSource::from_local_module(&m, source_dir).expect("Malformed module manifest"); let registry = crate::registry::RegistryList::with_default_registry(); let res = resolve_single_root_with_defaults(®istry, ms, Rc::clone(&m))?; diff --git a/crates/mooncake/src/resolver/mvs.rs b/crates/mooncake/src/resolver/mvs.rs index 21bc63e5..0faf8353 100644 --- a/crates/mooncake/src/resolver/mvs.rs +++ b/crates/mooncake/src/resolver/mvs.rs @@ -499,6 +499,7 @@ mod test { alert_list: None, include: None, exclude: None, + backend: WasmGC, } "#]] .assert_debug_eq(module_info); diff --git a/crates/moonutil/src/common.rs b/crates/moonutil/src/common.rs index adacaf3d..2f71c673 100644 --- a/crates/moonutil/src/common.rs +++ b/crates/moonutil/src/common.rs @@ -110,6 +110,8 @@ pub enum MoonModJSONFormatErrorKind { Source(#[from] SourceError), #[error("`version` bad format")] Version(#[from] semver::Error), + #[error("`backend` bad format")] + Backend(#[from] anyhow::Error), } pub fn read_module_from_json(path: &Path) -> Result { diff --git a/crates/moonutil/src/module.rs b/crates/moonutil/src/module.rs index b986c8e1..e410cf49 100644 --- a/crates/moonutil/src/module.rs +++ b/crates/moonutil/src/module.rs @@ -16,10 +16,15 @@ // // For inquiries, you can contact us via e-mail at jichuruanjian@idea.edu.cn. -use crate::common::{MoonModJSONFormatErrorKind, MooncOpt, NameError, MOON_PKG_JSON}; -use crate::dependency::{ - BinaryDependencyInfo, BinaryDependencyInfoJson, SourceDependencyInfo, SourceDependencyInfoJson, -}; +use crate::common::MoonModJSONFormatErrorKind; +use crate::common::MooncOpt; +use crate::common::NameError; +use crate::common::TargetBackend; +use crate::common::MOON_PKG_JSON; +use crate::dependency::BinaryDependencyInfo; +use crate::dependency::BinaryDependencyInfoJson; +use crate::dependency::SourceDependencyInfo; +use crate::dependency::SourceDependencyInfoJson; use crate::package::{AliasJSON, Package, PackageJSON}; use crate::path::ImportPath; use anyhow::bail; @@ -515,6 +520,8 @@ pub struct MoonMod { pub include: Option>, pub exclude: Option>, + + pub backend: TargetBackend, } #[derive(Debug, Serialize, Deserialize, JsonSchema)] @@ -600,6 +607,10 @@ pub struct MoonModJSON { /// Files to exclude when publishing. #[serde(skip_serializing_if = "Option::is_none")] pub exclude: Option>, + + /// Restrict backend to build the module. + #[serde(skip_serializing_if = "Option::is_none")] + pub backend: Option, } impl TryFrom for MoonMod { @@ -627,6 +638,12 @@ impl TryFrom for MoonMod { let source = j.source.map(|s| if s.is_empty() { ".".into() } else { s }); + let backend = if let Some(ref b) = j.backend { + TargetBackend::str_to_backend(b).map_err(MoonModJSONFormatErrorKind::Backend)? + } else { + TargetBackend::WasmGC + }; + Ok(MoonMod { name: j.name, version, @@ -649,6 +666,8 @@ impl TryFrom for MoonMod { include: j.include, exclude: j.exclude, + + backend, }) } } @@ -678,6 +697,12 @@ pub fn convert_module_to_mod_json(m: MoonMod) -> MoonModJSON { include: m.include, exclude: m.exclude, + + backend: if m.backend == TargetBackend::WasmGC { + None + } else { + Some(m.backend.to_flag().to_string()) + }, } } diff --git a/docs/manual-zh/src/source/mod_json_schema.html b/docs/manual-zh/src/source/mod_json_schema.html index 160fe0c3..64ab3c6c 100644 --- a/docs/manual-zh/src/source/mod_json_schema.html +++ b/docs/manual-zh/src/source/mod_json_schema.html @@ -52,6 +52,13 @@ "null" ] }, + "backend": { + "description": "Restrict backend to build the module.", + "type": [ + "string", + "null" + ] + }, "bin-deps": { "description": "third-party binary dependencies of the module", "type": [ diff --git a/docs/manual/src/source/mod_json_schema.html b/docs/manual/src/source/mod_json_schema.html index 160fe0c3..64ab3c6c 100644 --- a/docs/manual/src/source/mod_json_schema.html +++ b/docs/manual/src/source/mod_json_schema.html @@ -52,6 +52,13 @@ "null" ] }, + "backend": { + "description": "Restrict backend to build the module.", + "type": [ + "string", + "null" + ] + }, "bin-deps": { "description": "third-party binary dependencies of the module", "type": [