diff --git a/.github/workflows/wasm_test.yaml b/.github/workflows/wasm_test.yaml new file mode 100644 index 000000000..2b2396ecc --- /dev/null +++ b/.github/workflows/wasm_test.yaml @@ -0,0 +1,29 @@ +name: build-and-test-wasm +on: ["push", "pull_request"] +jobs: + build-and-test: + # Ref: https://github.com/actions/runner-images/tree/main/images/linux + name: Test + runs-on: ubuntu-latest + steps: + - name: Check out code + uses: actions/checkout@v3 + with: + submodules: "true" + + - name: Install rust nightly toolchain + uses: actions-rs/toolchain@v1 + with: + toolchain: 1.76 + override: true + components: clippy, rustfmt + + - name: Unit test + working-directory: ./kclvm + run: rustup target add wasm32-wasi && make build-wasm + shell: bash + + - uses: actions/upload-artifact@v3 + with: + name: kcl-wasm + path: kclvm/target/wasm32-wasi/release/kclvm_cli_cdylib.wasm diff --git a/kclvm/api/src/service/capi.rs b/kclvm/api/src/service/capi.rs index bdd1e75fa..ff5e76f5a 100644 --- a/kclvm/api/src/service/capi.rs +++ b/kclvm/api/src/service/capi.rs @@ -184,6 +184,7 @@ pub(crate) fn kclvm_get_service_fn_ptr_by_name(name: &str) -> u64 { "KclvmService.Rename" => rename as *const () as u64, "KclvmService.RenameCode" => rename_code as *const () as u64, "KclvmService.Test" => test as *const () as u64, + #[cfg(not(target_arch = "wasm32"))] "KclvmService.UpdateDependencies" => update_dependencies as *const () as u64, _ => panic!("unknown method name : {name}"), } @@ -524,6 +525,7 @@ pub(crate) fn test( call!(serv, args, result_len, TestArgs, test) } +#[cfg(not(target_arch = "wasm32"))] /// Service for the dependencies updating /// calling information. /// diff --git a/kclvm/api/src/service/service_impl.rs b/kclvm/api/src/service/service_impl.rs index a114b7671..cce3184e5 100644 --- a/kclvm/api/src/service/service_impl.rs +++ b/kclvm/api/src/service/service_impl.rs @@ -1,6 +1,6 @@ use std::collections::HashMap; use std::io::Write; -use std::path::{Path, PathBuf}; +use std::path::PathBuf; use std::string::String; use crate::gpyrpc::*; @@ -9,7 +9,6 @@ use anyhow::anyhow; use kcl_language_server::rename; use kclvm_config::settings::build_settings_pathbuf; use kclvm_driver::canonicalize_input_files; -use kclvm_driver::client::ModClient; use kclvm_loader::option::list_options; use kclvm_loader::{load_packages_with_cache, LoadPackageOptions}; use kclvm_parser::load_program; @@ -957,6 +956,7 @@ impl KclvmServiceImpl { Ok(result) } + #[cfg(not(target_arch = "wasm32"))] /// update_dependencies provides users with the ability to update kcl module dependencies. /// /// # Examples @@ -986,6 +986,8 @@ impl KclvmServiceImpl { &self, args: &UpdateDependenciesArgs, ) -> anyhow::Result { + use kclvm_driver::client::ModClient; + use std::path::Path; let mut client = ModClient::new(&args.manifest_path)?; if args.vendor { client.set_vendor(&Path::new(&args.manifest_path).join("vendor")); diff --git a/kclvm/driver/Cargo.toml b/kclvm/driver/Cargo.toml index 92aacfb6c..47a43a80b 100644 --- a/kclvm/driver/Cargo.toml +++ b/kclvm/driver/Cargo.toml @@ -19,10 +19,12 @@ walkdir = "2" serde = { version = "1.0", features = ["derive"] } anyhow = { version = "1.0.70", features = ["backtrace"] } glob = "0.3.1" -oci-distribution = { default-features = false, version = "0.11.0", features = ["rustls-tls"] } flate2 = "1.0.30" tar = "0.4.40" -tokio = { version = "1.37.0", features = ["full"] } indexmap = "2.2.6" once_cell = "1.19.0" parking_lot = "0.12.3" + +[target.'cfg(not(target_arch = "wasm32"))'.dependencies] +oci-distribution = { default-features = false, version = "0.11.0", features = ["rustls-tls"] } +tokio = { version = "1.37.0", features = ["full"] } diff --git a/kclvm/driver/src/lib.rs b/kclvm/driver/src/lib.rs index 958efb433..7a4a45ae1 100644 --- a/kclvm/driver/src/lib.rs +++ b/kclvm/driver/src/lib.rs @@ -1,4 +1,5 @@ pub mod arguments; +#[cfg(not(target_arch = "wasm32"))] pub mod client; pub mod toolchain; diff --git a/kclvm/driver/src/toolchain.rs b/kclvm/driver/src/toolchain.rs index dc343d83f..dc14e156f 100644 --- a/kclvm/driver/src/toolchain.rs +++ b/kclvm/driver/src/toolchain.rs @@ -1,14 +1,13 @@ -use crate::client::ModClient; use crate::{kcl, lookup_the_nearest_file_dir}; use anyhow::{bail, Result}; use kclvm_config::modfile::KCL_MOD_FILE; use kclvm_parser::LoadProgramOptions; use kclvm_utils::pkgpath::rm_external_pkg_name; -use parking_lot::Mutex; use serde::{Deserialize, Serialize}; use std::ffi::OsStr; -use std::sync::Arc; use std::{collections::HashMap, path::PathBuf, process::Command}; +#[cfg(not(target_arch = "wasm32"))] +use {crate::client::ModClient, parking_lot::Mutex, std::sync::Arc}; /// `Toolchain` is a trait that outlines a standard set of operations that must be /// implemented for a KCL module (mod), typically involving fetching metadata from, @@ -99,11 +98,13 @@ impl + Send + Sync> Toolchain for CommandToolchain { } } +#[cfg(not(target_arch = "wasm32"))] #[derive(Default)] pub struct NativeToolchain { client: Arc>, } +#[cfg(not(target_arch = "wasm32"))] impl Toolchain for NativeToolchain { fn fetch_metadata(&self, manifest_path: PathBuf) -> Result { let mut client = self.client.lock();