diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index bc5f2cb2..42b869a0 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -156,6 +156,10 @@ jobs: "$env:USERPROFILE\.moon\bin" | Out-File -FilePath $env:GITHUB_PATH -Append "$env:GITHUB_WORKSPACE\target\debug" | Out-File -FilePath $env:GITHUB_PATH -Append + - name: Setup MSVC + if: ${{ matrix.os == 'windows-latest' }} + uses: ilammy/msvc-dev-cmd@v1 + - name: Build run: cargo build - name: Versions @@ -230,6 +234,10 @@ jobs: Set-ExecutionPolicy RemoteSigned -Scope CurrentUser; irm https://cli.moonbitlang.com/install/powershell.ps1 | iex "C:\Users\runneradmin\.moon\bin" | Out-File -FilePath $env:GITHUB_PATH -Append + - name: Setup MSVC + if: ${{ matrix.os == 'windows-latest' }} + uses: ilammy/msvc-dev-cmd@v1 + - name: Build run: cargo build - name: Versions diff --git a/crates/moonutil/src/common.rs b/crates/moonutil/src/common.rs index 3eb77fe9..6f82e159 100644 --- a/crates/moonutil/src/common.rs +++ b/crates/moonutil/src/common.rs @@ -814,15 +814,20 @@ pub fn set_native_backend_link_flags( target_backend: Option, module: &mut crate::module::ModuleDB, ) { - #[cfg(unix)] match run_mode { // need link-core for build, test and run RunMode::Build | RunMode::Test | RunMode::Run => { - if release && target_backend == Some(TargetBackend::Native) { - // check if cc exists in PATH - if let Err(e) = which::which("cc") { + if target_backend == Some(TargetBackend::Native) { + // check if c compiler exists in PATH + #[cfg(unix)] + let compiler = "cc"; + #[cfg(windows)] + let compiler = "cl"; + + if let Err(e) = which::which(compiler) { eprintln!( - "error: 'cc' not found in PATH, which is used for native backend release compilation: {}", + "error: '{}' not found in PATH, which is used for native backend release compilation: {}", + compiler, e ); std::process::exit(1); @@ -841,19 +846,47 @@ pub fn set_native_backend_link_flags( let all_pkgs = module.get_all_packages_mut(); for (_, pkg) in all_pkgs { - if pkg.link.is_none() || pkg.link.as_ref().unwrap().native.is_none() { - pkg.link = Some(crate::package::Link { - native: Some(crate::package::NativeLinkConfig { - cc: Some("cc".to_string()), - cc_flags: Some(format!( - "-O2 {} -fwrapv", - libmoonbitrun_path.as_ref().unwrap().display() - )), - cc_link_flags: Some("-lm".to_string()), + let existing_native = pkg.link.as_ref().and_then(|link| link.native.as_ref()); + + let native_config = crate::package::NativeLinkConfig { + cc: existing_native + .and_then(|n| n.cc.clone()) + .or_else(|| release.then_some(compiler.to_string())), + + cc_flags: existing_native + .and_then(|n| n.cc_flags.clone()) + .or_else(|| { + release + .then(|| { + #[cfg(unix)] + return Some(format!( + "-O2 {} -fwrapv", + libmoonbitrun_path.as_ref().unwrap().display() + )); + #[cfg(windows)] + return None; + }) + .flatten() }), - ..Default::default() - }); - } + + cc_link_flags: existing_native + .and_then(|n| n.cc_link_flags.clone()) + .or_else(|| { + release + .then(|| { + #[cfg(unix)] + return Some("-lm".to_string()); + #[cfg(windows)] + return None; + }) + .flatten() + }), + }; + + pkg.link = Some(crate::package::Link { + native: Some(native_config), + ..Default::default() + }); } } } diff --git a/crates/moonutil/src/package.rs b/crates/moonutil/src/package.rs index bc52f979..0f24e32b 100644 --- a/crates/moonutil/src/package.rs +++ b/crates/moonutil/src/package.rs @@ -338,7 +338,7 @@ pub struct WasmLinkConfig { pub flags: Option>, } -#[derive(Debug, Serialize, Deserialize, Clone, JsonSchema)] +#[derive(Debug, Serialize, Deserialize, Clone, JsonSchema, Default)] #[serde(rename_all = "kebab-case")] pub struct NativeLinkConfig { #[serde(skip_serializing_if = "Option::is_none")]