Skip to content

Commit

Permalink
Merge pull request #428 from moonbitlang/native_flags
Browse files Browse the repository at this point in the history
internal: polish native link flags
  • Loading branch information
Young-Flash authored Oct 29, 2024
2 parents c92bada + f2e013f commit 3337443
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 18 deletions.
8 changes: 8 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
67 changes: 50 additions & 17 deletions crates/moonutil/src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -814,15 +814,20 @@ pub fn set_native_backend_link_flags(
target_backend: Option<TargetBackend>,
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);
Expand All @@ -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()
});
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion crates/moonutil/src/package.rs
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ pub struct WasmLinkConfig {
pub flags: Option<Vec<String>>,
}

#[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")]
Expand Down

0 comments on commit 3337443

Please sign in to comment.