Skip to content

Commit

Permalink
feat: support set backend in mod.json
Browse files Browse the repository at this point in the history
  • Loading branch information
Young-Flash committed Dec 19, 2024
1 parent 5a38a2f commit 6abe2d9
Show file tree
Hide file tree
Showing 17 changed files with 117 additions and 6 deletions.
2 changes: 1 addition & 1 deletion crates/moon/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
3 changes: 3 additions & 0 deletions crates/moon/tests/test_cases/backend_in_mod_json.in/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
## MoonBit Unicode

We can use emoji in MoonBit now :)
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
test {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
fn main {
let 🤣 = 3
println(🤣)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"is-main": true,
"import": {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"name": "unicode_demo",
"backend": "js"
}
39 changes: 39 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,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
"#]],
);
}
2 changes: 2 additions & 0 deletions crates/moonbuild/src/bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
2 changes: 2 additions & 0 deletions crates/moonbuild/src/new.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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))?;
Expand Down
7 changes: 7 additions & 0 deletions crates/moonbuild/template/mod.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": [
Expand Down
2 changes: 1 addition & 1 deletion crates/mooncake/src/pkg/remove.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ pub fn remove(
pkgname,
)
}
let m = Rc::new(m);
let m: Rc<moonutil::module::MoonMod> = 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(&registry, ms, Rc::clone(&m))?;
Expand Down
1 change: 1 addition & 0 deletions crates/mooncake/src/resolver/mvs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -499,6 +499,7 @@ mod test {
alert_list: None,
include: None,
exclude: None,
backend: WasmGC,
}
"#]]
.assert_debug_eq(module_info);
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 @@ -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<MoonMod, MoonModJSONFormatError> {
Expand Down
33 changes: 29 additions & 4 deletions crates/moonutil/src/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,15 @@
//
// For inquiries, you can contact us via e-mail at [email protected].

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;
Expand Down Expand Up @@ -515,6 +520,8 @@ pub struct MoonMod {

pub include: Option<Vec<String>>,
pub exclude: Option<Vec<String>>,

pub backend: TargetBackend,
}

#[derive(Debug, Serialize, Deserialize, JsonSchema)]
Expand Down Expand Up @@ -600,6 +607,10 @@ pub struct MoonModJSON {
/// Files to exclude when publishing.
#[serde(skip_serializing_if = "Option::is_none")]
pub exclude: Option<Vec<String>>,

/// Restrict backend to build the module.
#[serde(skip_serializing_if = "Option::is_none")]
pub backend: Option<String>,
}

impl TryFrom<MoonModJSON> for MoonMod {
Expand Down Expand Up @@ -627,6 +638,12 @@ impl TryFrom<MoonModJSON> 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,
Expand All @@ -649,6 +666,8 @@ impl TryFrom<MoonModJSON> for MoonMod {

include: j.include,
exclude: j.exclude,

backend,
})
}
}
Expand Down Expand Up @@ -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())
},
}
}

Expand Down
7 changes: 7 additions & 0 deletions docs/manual-zh/src/source/mod_json_schema.html
Original file line number Diff line number Diff line change
Expand Up @@ -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": [
Expand Down
7 changes: 7 additions & 0 deletions docs/manual/src/source/mod_json_schema.html
Original file line number Diff line number Diff line change
Expand Up @@ -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": [
Expand Down

0 comments on commit 6abe2d9

Please sign in to comment.