Repository: WasmEdge/wasmedge-rust-sdk (language: rust)

Issue:
Build failure on alpine
`wasmedge-rust-sdk` fails to build on alpine since the `libc` crate doesn't have `libc::pthread_sigqueue` for `musl` targets.
```
error[E0425]: cannot find function `pthread_sigqueue` in crate `libc`
311.1     --> /usr/local/cargo/registry/src/index.crates.io-6f17d22bba15001f/wasmedge-sys-0.17.1/src/executor.rs:38:19
311.1      |
311.1 38   |             libc::pthread_sigqueue(dist_pthread, sig, si_value);
311.1      |                   ^^^^^^^^^^^^^^^^ help: a function with a similar name exists: `pthread_sigmask`
311.1      |
311.1     ::: /usr/local/cargo/registry/src/index.crates.io-6f17d22bba15001f/libc-0.2.147/src/unix/linux_like/linux/mod.rs:4623:5
311.1      |
311.1 4623 |     pub fn pthread_sigmask(how: ::c_int, set: *const sigset_t, oldset: *mut sigset_t) -> ::c_int;
311.1      |     -------------------------------------------------------------------------------------------- similarly named function `pthread_sigmask` defined here
311.1 
313.4 For more information about this error, try `rustc --explain E0425`.
```

Could this be gated behind a feature flag, or turned off for musl libc?


Discussion / hints:

Fix patch:
diff --git a/crates/wasmedge-sys/src/executor.rs b/crates/wasmedge-sys/src/executor.rs
index 9fd9dbbe8..6ca78f5ba 100644
--- a/crates/wasmedge-sys/src/executor.rs
+++ b/crates/wasmedge-sys/src/executor.rs
@@ -2,27 +2,31 @@
 
 use super::ffi;
 #[cfg(all(feature = "async", target_os = "linux"))]
-use crate::r#async::fiber::{AsyncState, FiberFuture, TimeoutFiberFuture};
+use crate::r#async::fiber::{AsyncState, FiberFuture};
+
+#[cfg(all(feature = "async", target_os = "linux", not(target_env = "musl")))]
+use crate::r#async::fiber::TimeoutFiberFuture;
+
 use crate::{
     instance::module::InnerInstance, types::WasmEdgeString, utils::check, Config, Engine, FuncRef,
     Function, ImportModule, Instance, Module, Statistics, Store, WasiInstance, WasmEdgeResult,
     WasmValue,
 };
 use parking_lot::Mutex;
-#[cfg(target_os = "linux")]
+#[cfg(all(target_os = "linux", not(target_env = "musl")))]
 use std::os::raw::c_void;
 use std::sync::Arc;
 use wasmedge_types::error::WasmEdgeError;
 
-#[cfg(target_os = "linux")]
+#[cfg(all(target_os = "linux", not(target_env = "musl")))]
 pub(crate) struct JmpState {
     pub(crate) sigjmp_buf: *mut setjmp::sigjmp_buf,
 }
 
-#[cfg(target_os = "linux")]
+#[cfg(all(target_os = "linux", not(target_env = "musl")))]
 scoped_tls::scoped_thread_local!(pub(crate) static JMP_BUF: JmpState);
 
-#[cfg(target_os = "linux")]
+#[cfg(all(target_os = "linux", not(target_env = "musl")))]
 unsafe extern "C" fn sync_timeout(sig: i32, info: *mut libc::siginfo_t) {
     if let Some(info) = info.as_mut() {
         let si_value = info.si_value();
@@ -39,7 +43,7 @@ unsafe extern "C" fn sync_timeout(sig: i32, info: *mut libc::siginfo_t) {
         }
     }
 }
-#[cfg(target_os = "linux")]
+#[cfg(all(target_os = "linux", not(target_env = "musl")))]
 unsafe extern "C" fn pre_host_func(_: *mut c_void) {
     use libc::SIG_BLOCK;
 
@@ -48,7 +52,7 @@ unsafe extern "C" fn pre_host_func(_: *mut c_void) {
     libc::sigaddset(&mut set, timeout_signo());
     libc::pthread_sigmask(SIG_BLOCK, &set, std::ptr::null_mut());
 }
-#[cfg(target_os = "linux")]
+#[cfg(all(target_os = "linux", not(target_env = "musl")))]
 unsafe extern "C" fn post_host_func(_: *mut c_void) {
     use libc::SIG_UNBLOCK;
 
@@ -59,7 +63,7 @@ unsafe extern "C" fn post_host_func(_: *mut c_void) {
 }
 
 #[inline]
-#[cfg(target_os = "linux")]
+#[cfg(all(target_os = "linux", not(target_env = "musl")))]
 pub(crate) fn timeout_signo() -> i32 {
     option_env!("SIG_OFFSET")
         .and_then(|s| s.parse().ok())
@@ -67,11 +71,11 @@ pub(crate) fn timeout_signo() -> i32 {
         + libc::SIGRTMIN()
 }
 
-#[cfg(target_os = "linux")]
+#[cfg(all(target_os = "linux", not(target_env = "musl")))]
 static INIT_SIGNAL_LISTEN: std::sync::Once = std::sync::Once::new();
 
 #[inline(always)]
-#[cfg(target_os = "linux")]
+#[cfg(all(target_os = "linux", not(target_env = "musl")))]
 pub(crate) unsafe fn init_signal_listen() {
     INIT_SIGNAL_LISTEN.call_once(|| {
         let mut new_act: libc::sigaction = std::mem::zeroed();
@@ -120,7 +124,7 @@ impl Executor {
         match ctx.is_null() {
             true => Err(Box::new(WasmEdgeError::ExecutorCreate)),
             false => {
-                #[cfg(target_os = "linux")]
+                #[cfg(all(target_os = "linux", not(target_env = "musl")))]
                 unsafe {
                     ffi::WasmEdge_ExecutorExperimentalRegisterPreHostFunction(
                         ctx,
@@ -364,8 +368,8 @@ impl Executor {
     /// # Errors
     ///
     /// If fail to run the host function, then an error is returned.
-    #[cfg(target_os = "linux")]
-    #[cfg_attr(docsrs, doc(cfg(target_os = "linux")))]
+    #[cfg(all(target_os = "linux", not(target_env = "musl")))]
+    #[cfg_attr(docsrs, doc(cfg(all(target_os = "linux", not(target_env = "musl")))))]
     pub fn call_func_with_timeout(
         &self,
         func: &Function,
@@ -467,8 +471,11 @@ impl Executor {
     /// # Errors
     ///
     /// If fail to run the host function, then an error is returned.
-    #[cfg(all(feature = "async", target_os = "linux"))]
-    #[cfg_attr(docsrs, doc(cfg(all(feature = "async", target_os = "linux"))))]
+    #[cfg(all(feature = "async", target_os = "linux", not(target_env = "musl")))]
+    #[cfg_attr(
+        docsrs,
+        doc(cfg(all(feature = "async", target_os = "linux", not(target_env = "musl"))))
+    )]
     #[cfg(feature = "async")]
     pub async fn call_func_async_with_timeout(
         &self,
diff --git a/src/executor.rs b/src/executor.rs
index 4378bc5ae..8a5a258e7 100644
--- a/src/executor.rs
+++ b/src/executor.rs
@@ -71,8 +71,8 @@ impl Executor {
     /// # Errors
     ///
     /// If fail to run the host function, then an error is returned.
-    #[cfg(target_os = "linux")]
-    #[cfg_attr(docsrs, doc(cfg(target_os = "linux")))]
+    #[cfg(all(target_os = "linux", not(target_env = "musl")))]
+    #[cfg_attr(docsrs, doc(cfg(all(target_os = "linux", not(target_env = "musl")))))]
     pub fn run_func_with_timeout(
         &self,
         func: &Func,
@@ -124,8 +124,11 @@ impl Executor {
     /// # Errors
     ///
     /// If fail to run the host function, then an error is returned.
-    #[cfg(all(feature = "async", target_os = "linux"))]
-    #[cfg_attr(docsrs, doc(cfg(all(feature = "async", target_os = "linux"))))]
+    #[cfg(all(feature = "async", target_os = "linux", not(target_env = "musl")))]
+    #[cfg_attr(
+        docsrs,
+        doc(cfg(all(feature = "async", target_os = "linux", not(target_env = "musl"))))
+    )]
     pub async fn run_func_async_with_timeout(
         &self,
         async_state: &AsyncState,
diff --git a/src/externals/function.rs b/src/externals/function.rs
index 774c66302..e1b792db1 100644
--- a/src/externals/function.rs
+++ b/src/externals/function.rs
@@ -256,8 +256,8 @@ impl Func {
     /// # Error
     ///
     /// If fail to run the host function, then an error is returned.
-    #[cfg(target_os = "linux")]
-    #[cfg_attr(docsrs, doc(cfg(target_os = "linux")))]
+    #[cfg(all(target_os = "linux", not(target_env = "musl")))]
+    #[cfg_attr(docsrs, doc(cfg(all(target_os = "linux", not(target_env = "musl")))))]
     pub fn run_with_timeout(
         &self,
         executor: &Executor,
@@ -302,8 +302,11 @@ impl Func {
     /// # Error
     ///
     /// If fail to run the host function, then an error is returned.
-    #[cfg(all(feature = "async", target_os = "linux"))]
-    #[cfg_attr(docsrs, doc(cfg(all(feature = "async", target_os = "linux"))))]
+    #[cfg(all(feature = "async", target_os = "linux", not(target_env = "musl")))]
+    #[cfg_attr(
+        docsrs,
+        doc(cfg(all(feature = "async", target_os = "linux", not(target_env = "musl"))))
+    )]
     pub async fn run_async_with_timeout(
         &self,
         async_state: &AsyncState,
diff --git a/src/vm.rs b/src/vm.rs
index ca4c483cc..71173f666 100644
--- a/src/vm.rs
+++ b/src/vm.rs
@@ -501,7 +501,7 @@ impl Vm {
     /// # Error
     ///
     /// If fail to run the wasm function, then an error is returned.
-    #[cfg(target_os = "linux")]
+    #[cfg(all(target_os = "linux", not(target_env = "musl")))]
     pub fn run_func_with_timeout(
         &self,
         mod_name: Option<&str>,
@@ -594,8 +594,11 @@ impl Vm {
     /// # Error
     ///
     /// If fail to run the wasm function, then an error is returned.
-    #[cfg(all(feature = "async", target_os = "linux"))]
-    #[cfg_attr(docsrs, doc(cfg(all(feature = "async", target_os = "linux"))))]
+    #[cfg(all(feature = "async", target_os = "linux", not(target_env = "musl")))]
+    #[cfg_attr(
+        docsrs,
+        doc(cfg(all(feature = "async", target_os = "linux", not(target_env = "musl"))))
+    )]
     pub async fn run_func_async_with_timeout(
         &self,
         async_state: &AsyncState,


Test patch: