From 6ad1a3db29c4aca3a60fb3b1b55002b436b219d1 Mon Sep 17 00:00:00 2001 From: Christoph Herzog Date: Mon, 29 Sep 2025 17:50:27 +0200 Subject: [PATCH 1/4] fix(build): Meaningful backend compilation errors in api Currently, when no backend is enabled, we get a horrible mess of errors. This commit: * Makes sure the wasmer-compiler crate can be compiled withou the compiler feature enabled * Makes sure the compiler crate fails to compile with a single meaningful message if neither core nor std features are enabled * Makes sure the api crate fails to compile with a single meaninful error when no backend is enabled Note: our current module structure doesn't make it trivial to do this cleanly. I went for an approach that has more duplication, but is minimally invasive. --- lib/api/src/error.rs | 2 +- lib/api/src/lib.rs | 124 ++++++++++++++++++++++++++-- lib/compiler/src/engine/artifact.rs | 2 +- lib/compiler/src/engine/inner.rs | 2 +- lib/compiler/src/lib.rs | 10 +++ 5 files changed, 132 insertions(+), 8 deletions(-) diff --git a/lib/api/src/error.rs b/lib/api/src/error.rs index 35400775b7d..75c1e806ea5 100644 --- a/lib/api/src/error.rs +++ b/lib/api/src/error.rs @@ -20,7 +20,7 @@ pub enum LinkError { /// A trap ocurred during linking. #[cfg_attr(feature = "std", error("RuntimeError occurred during linking: {0}"))] - Trap(#[source] RuntimeError), + Trap(#[cfg_attr(feature = "std", source)] RuntimeError), /// Insufficient resources available for linking. #[cfg_attr(feature = "std", error("Insufficient resources: {0}"))] Resource(String), diff --git a/lib/api/src/lib.rs b/lib/api/src/lib.rs index 36270943339..db04888e480 100644 --- a/lib/api/src/lib.rs +++ b/lib/api/src/lib.rs @@ -413,29 +413,143 @@ //! [`wasmi`]: https://github.com/wasmi-labs/wasmi #[cfg(not(any( - feature = "sys", - feature = "js", - feature = "jsc", + feature = "singlepass", + feature = "cranelift", + feature = "llvm", feature = "wamr", + feature = "wasmi", feature = "v8", - feature = "wasmi" + feature = "js", + feature = "jsc" )))] compile_error!( - "One of: `sys`, `js`, `jsc` `wamr`, `wasmi` or `v8` features must be enabled. Please, pick one." + "wasmer requires enabling at least one backend feature: `sys`, `wamr`, `wasmi`, `v8`, `js`, or `jsc`." ); +#[cfg(all( + feature = "sys", + not(any(feature = "singlepass", feature = "cranelift", feature = "llvm")) +))] +compile_error!( + "the `sys` feature requires enabling at least one compiler backend: `singlepass`, `cranelift`, or `llvm`." +); + +#[cfg(any( + feature = "cranelift", + feature = "singlepass", + feature = "llvm", + feature = "js", + feature = "jsc", + feature = "wamr", + feature = "v8", + feature = "wasmi" +))] mod utils; + +#[cfg(any( + feature = "cranelift", + feature = "singlepass", + feature = "llvm", + feature = "js", + feature = "jsc", + feature = "wamr", + feature = "v8", + feature = "wasmi" +))] pub use utils::*; +#[cfg(any( + feature = "cranelift", + feature = "singlepass", + feature = "llvm", + feature = "js", + feature = "jsc", + feature = "wamr", + feature = "v8", + feature = "wasmi" +))] mod entities; + +#[cfg(any( + feature = "cranelift", + feature = "singlepass", + feature = "llvm", + feature = "js", + feature = "jsc", + feature = "wamr", + feature = "v8", + feature = "wasmi" +))] pub use entities::memory::{location::MemoryLocation, MemoryView}; + +#[cfg(any( + feature = "cranelift", + feature = "singlepass", + feature = "llvm", + feature = "js", + feature = "jsc", + feature = "wamr", + feature = "v8", + feature = "wasmi" +))] pub use entities::*; +#[cfg(any( + feature = "cranelift", + feature = "singlepass", + feature = "llvm", + feature = "js", + feature = "jsc", + feature = "wamr", + feature = "v8", + feature = "wasmi" +))] mod error; + +#[cfg(any( + feature = "cranelift", + feature = "singlepass", + feature = "llvm", + feature = "js", + feature = "jsc", + feature = "wamr", + feature = "v8", + feature = "wasmi" +))] pub use error::*; +#[cfg(any( + feature = "cranelift", + feature = "singlepass", + feature = "llvm", + feature = "js", + feature = "jsc", + feature = "wamr", + feature = "v8", + feature = "wasmi" +))] mod backend; +#[cfg(any( + feature = "cranelift", + feature = "singlepass", + feature = "llvm", + feature = "js", + feature = "jsc", + feature = "wamr", + feature = "v8", + feature = "wasmi" +))] pub use backend::*; +#[cfg(any( + feature = "cranelift", + feature = "singlepass", + feature = "llvm", + feature = "js", + feature = "jsc", + feature = "wamr", + feature = "v8", + feature = "wasmi" +))] mod vm; pub use wasmer_types::{ diff --git a/lib/compiler/src/engine/artifact.rs b/lib/compiler/src/engine/artifact.rs index faeeaea2fef..8ffa94129a1 100644 --- a/lib/compiler/src/engine/artifact.rs +++ b/lib/compiler/src/engine/artifact.rs @@ -448,7 +448,7 @@ impl Artifact { get_got_address(RelocationTarget::LibCall(wasmer_vm::LibCall::EHPersonality)), )?; - #[cfg(not(target_arch = "wasm32"))] + #[cfg(all(feature = "compiler", not(target_arch = "wasm32")))] { engine_inner.register_perfmap(&finished_functions, module_info)?; } diff --git a/lib/compiler/src/engine/inner.rs b/lib/compiler/src/engine/inner.rs index 8d1637fbadb..8ec297e4b62 100644 --- a/lib/compiler/src/engine/inner.rs +++ b/lib/compiler/src/engine/inner.rs @@ -536,7 +536,7 @@ impl EngineInner { .register_frame_info(frame_info); } - #[cfg(not(target_arch = "wasm32"))] + #[cfg(all(feature = "compiler", not(target_arch = "wasm32")))] pub(crate) fn register_perfmap( &self, finished_functions: &PrimaryMap, diff --git a/lib/compiler/src/lib.rs b/lib/compiler/src/lib.rs index e949a4329ae..56c4d5a0146 100644 --- a/lib/compiler/src/lib.rs +++ b/lib/compiler/src/lib.rs @@ -32,6 +32,7 @@ compile_error!("Both the `std` and `core` features are disabled. Please enable o extern crate alloc; #[allow(unused_imports)] +#[cfg(any(feature = "std", feature = "core"))] mod lib { #[cfg(feature = "core")] pub mod std { @@ -46,18 +47,27 @@ mod lib { } } +#[cfg(any(feature = "std", feature = "core"))] mod engine; +#[cfg(any(feature = "std", feature = "core"))] mod traits; +#[cfg(any(feature = "std", feature = "core"))] pub mod object; +#[cfg(any(feature = "std", feature = "core"))] pub mod serialize; +#[cfg(any(feature = "std", feature = "core"))] pub mod types; +#[cfg(any(feature = "std", feature = "core"))] pub use crate::engine::*; +#[cfg(any(feature = "std", feature = "core"))] pub use crate::traits::*; +#[cfg(any(feature = "std", feature = "core"))] mod artifact_builders; +#[cfg(any(feature = "std", feature = "core"))] pub use self::artifact_builders::*; #[cfg(feature = "compiler")] From 2b4fe02314b9ecaebe9c39c403275144e55bfe48 Mon Sep 17 00:00:00 2001 From: Martin Liska Date: Tue, 25 Nov 2025 17:09:24 +0100 Subject: [PATCH 2/4] use declarative macro for the #[cfg] conditions --- lib/api/src/lib.rs | 135 +++++++++------------------------------- lib/compiler/src/lib.rs | 44 +++++++------ 2 files changed, 54 insertions(+), 125 deletions(-) diff --git a/lib/api/src/lib.rs b/lib/api/src/lib.rs index 548ccc5741c..1ed0a068d07 100644 --- a/lib/api/src/lib.rs +++ b/lib/api/src/lib.rs @@ -412,6 +412,24 @@ //! [`wamr`]: https://github.com/bytecodealliance/wasm-micro-runtime //! [`wasmi`]: https://github.com/wasmi-labs/wasmi +macro_rules! cfg_compiler { + ($($item:item)*) => { + $( + #[cfg(any( + feature = "cranelift", + feature = "singlepass", + feature = "llvm", + feature = "js", + feature = "jsc", + feature = "wamr", + feature = "v8", + feature = "wasmi" + ))] + $item + )* + }; +} + #[cfg(not(any( feature = "singlepass", feature = "cranelift", @@ -434,30 +452,19 @@ compile_error!( "the `sys` feature requires enabling at least one compiler backend: `singlepass`, `cranelift`, or `llvm`." ); -#[cfg(any( - feature = "cranelift", - feature = "singlepass", - feature = "llvm", - feature = "js", - feature = "jsc", - feature = "wamr", - feature = "v8", - feature = "wasmi" -))] -mod utils; - -#[cfg(any( - feature = "cranelift", - feature = "singlepass", - feature = "llvm", - feature = "js", - feature = "jsc", - feature = "wamr", - feature = "v8", - feature = "wasmi" -))] -pub use utils::*; +cfg_compiler! { + mod utils; + pub use utils::*; + pub use entities::memory::{MemoryView, location::MemoryLocation}; + mod error; + pub use error::*; + pub use entities::*; + mod backend; + pub use backend::*; + mod vm; +} +// TODO: cannot be placed into cfg_compiler due to: error: `inner` is ambiguous #[cfg(any( feature = "cranelift", feature = "singlepass", @@ -470,88 +477,6 @@ pub use utils::*; ))] mod entities; -#[cfg(any( - feature = "cranelift", - feature = "singlepass", - feature = "llvm", - feature = "js", - feature = "jsc", - feature = "wamr", - feature = "v8", - feature = "wasmi" -))] -pub use entities::memory::{MemoryView, location::MemoryLocation}; - -#[cfg(any( - feature = "cranelift", - feature = "singlepass", - feature = "llvm", - feature = "js", - feature = "jsc", - feature = "wamr", - feature = "v8", - feature = "wasmi" -))] -pub use entities::*; - -#[cfg(any( - feature = "cranelift", - feature = "singlepass", - feature = "llvm", - feature = "js", - feature = "jsc", - feature = "wamr", - feature = "v8", - feature = "wasmi" -))] -mod error; - -#[cfg(any( - feature = "cranelift", - feature = "singlepass", - feature = "llvm", - feature = "js", - feature = "jsc", - feature = "wamr", - feature = "v8", - feature = "wasmi" -))] -pub use error::*; - -#[cfg(any( - feature = "cranelift", - feature = "singlepass", - feature = "llvm", - feature = "js", - feature = "jsc", - feature = "wamr", - feature = "v8", - feature = "wasmi" -))] -mod backend; -#[cfg(any( - feature = "cranelift", - feature = "singlepass", - feature = "llvm", - feature = "js", - feature = "jsc", - feature = "wamr", - feature = "v8", - feature = "wasmi" -))] -pub use backend::*; -#[cfg(any( - feature = "cranelift", - feature = "singlepass", - feature = "llvm", - feature = "js", - feature = "jsc", - feature = "wamr", - feature = "v8", - feature = "wasmi" -))] -mod vm; - pub use wasmer_types::{ Bytes, CompileError, DeserializeError, ExportIndex, ExportType, ExternType, FrameInfo, FunctionType, GlobalInit, GlobalType, ImportType, LocalFunctionIndex, MemoryError, MemoryStyle, diff --git a/lib/compiler/src/lib.rs b/lib/compiler/src/lib.rs index cd500453469..c65c1bade45 100644 --- a/lib/compiler/src/lib.rs +++ b/lib/compiler/src/lib.rs @@ -20,6 +20,18 @@ )] #![cfg_attr(docsrs, feature(doc_cfg, doc_auto_cfg))] +macro_rules! cfg_std_or_core { + ($($item:item)*) => { + $( + #[cfg(any( + feature = "std", + feature = "core", + ))] + $item + )* + }; +} + #[cfg(all(feature = "std", feature = "core"))] compile_error!( "The `std` and `core` features are both enabled, which is an error. Please enable only once." @@ -47,30 +59,22 @@ mod lib { } } -#[cfg(any(feature = "std", feature = "core"))] -mod engine; -#[cfg(any(feature = "std", feature = "core"))] -mod traits; +cfg_std_or_core! { + mod engine; + mod traits; -#[cfg(any(feature = "std", feature = "core"))] -pub mod misc; -#[cfg(any(feature = "std", feature = "core"))] -pub mod object; -#[cfg(any(feature = "std", feature = "core"))] -pub mod serialize; -#[cfg(any(feature = "std", feature = "core"))] -pub mod types; + pub mod misc; + pub mod object; + pub mod serialize; + pub mod types; -#[cfg(any(feature = "std", feature = "core"))] -pub use crate::engine::*; -#[cfg(any(feature = "std", feature = "core"))] -pub use crate::traits::*; + pub use crate::engine::*; + pub use crate::traits::*; -#[cfg(any(feature = "std", feature = "core"))] -mod artifact_builders; + mod artifact_builders; -#[cfg(any(feature = "std", feature = "core"))] -pub use self::artifact_builders::*; + pub use self::artifact_builders::*; +} #[cfg(feature = "compiler")] mod compiler; From da963364e38a03a54eaf266ad07f0358790bb489 Mon Sep 17 00:00:00 2001 From: Martin Liska Date: Wed, 26 Nov 2025 11:02:19 +0100 Subject: [PATCH 3/4] fix build errors --- Makefile | 4 ++-- lib/api/src/lib.rs | 2 +- lib/cache/Cargo.toml | 2 +- lib/cli/Cargo.toml | 1 + lib/middlewares/Cargo.toml | 1 + lib/swift/Cargo.toml | 2 +- lib/sys-utils/Cargo.toml | 1 + 7 files changed, 8 insertions(+), 5 deletions(-) diff --git a/Makefile b/Makefile index 9c6057a9ffd..86ab2173343 100644 --- a/Makefile +++ b/Makefile @@ -606,10 +606,10 @@ build-capi-jsc: build-capi-headless: ifeq ($(CARGO_TARGET_FLAG),) CARGO_TARGET_DIR=target/headless RUSTFLAGS="${RUSTFLAGS} -C panic=abort -C link-dead-code -C lto -O -C embed-bitcode=yes" $(CARGO_BINARY) build --target $(HOST_TARGET) --manifest-path lib/c-api/Cargo.toml --release \ - --no-default-features --features compiler-headless,wasi,webc_runner --locked + --no-default-features --features compiler-headless,wasi,webc_runner,wasmer-api/cranelift --locked else CARGO_TARGET_DIR=target/headless RUSTFLAGS="${RUSTFLAGS} -C panic=abort -C link-dead-code -C lto -O -C embed-bitcode=yes" $(CARGO_BINARY) build $(CARGO_TARGET_FLAG) --manifest-path lib/c-api/Cargo.toml --release \ - --no-default-features --features compiler-headless,wasi,webc_runner --locked + --no-default-features --features compiler-headless,wasi,webc_runner,wasmer-api/cranelift --locked endif build-capi-headless-ios: diff --git a/lib/api/src/lib.rs b/lib/api/src/lib.rs index 1ed0a068d07..7280c22a583 100644 --- a/lib/api/src/lib.rs +++ b/lib/api/src/lib.rs @@ -441,7 +441,7 @@ macro_rules! cfg_compiler { feature = "jsc" )))] compile_error!( - "wasmer requires enabling at least one backend feature: `sys`, `wamr`, `wasmi`, `v8`, `js`, or `jsc`." + "wasmer requires enabling at least one backend feature: `singlepass`, `cranelift`, `llvm`, `wamr`, `wasmi`, `v8`, `js`, or `jsc`." ); #[cfg(all( diff --git a/lib/cache/Cargo.toml b/lib/cache/Cargo.toml index 4254a153c7d..c0438b56261 100644 --- a/lib/cache/Cargo.toml +++ b/lib/cache/Cargo.toml @@ -35,5 +35,5 @@ filesystem = [] blake3-pure = ["blake3/pure"] [package.metadata.docs.rs] -features = ["wasmer/sys", "wasmer/compiler"] +features = ["wasmer/sys", "wasmer/compiler", "wasmer/cranelift"] rustc-args = ["--cfg", "docsrs"] diff --git a/lib/cli/Cargo.toml b/lib/cli/Cargo.toml index 905286ee0d1..9da59ae1dcd 100644 --- a/lib/cli/Cargo.toml +++ b/lib/cli/Cargo.toml @@ -319,3 +319,4 @@ bin-dir = "bin/{ bin }.exe" [package.metadata.docs.rs] rustc-args = ["--cfg", "docsrs"] +features = ["wasmer/cranelift"] diff --git a/lib/middlewares/Cargo.toml b/lib/middlewares/Cargo.toml index bf67387479e..d95d3713e29 100644 --- a/lib/middlewares/Cargo.toml +++ b/lib/middlewares/Cargo.toml @@ -31,3 +31,4 @@ maintenance = { status = "actively-developed" } [package.metadata.docs.rs] rustc-args = ["--cfg", "docsrs"] +features = ["wasmer/cranelift"] diff --git a/lib/swift/Cargo.toml b/lib/swift/Cargo.toml index 8cfb6114fb4..9aa0c3718f5 100644 --- a/lib/swift/Cargo.toml +++ b/lib/swift/Cargo.toml @@ -33,5 +33,5 @@ wasmer-package.workspace = true uniffi = { version = "0.27", features = ["build"] } [package.metadata.docs.rs] -features = ["wasmer/sys", "wasmer/compiler"] +features = ["wasmer/sys", "wasmer/cranelift"] rustc-args = ["--cfg", "docsrs"] diff --git a/lib/sys-utils/Cargo.toml b/lib/sys-utils/Cargo.toml index 5ce20af23b5..6bd6a64f5da 100644 --- a/lib/sys-utils/Cargo.toml +++ b/lib/sys-utils/Cargo.toml @@ -28,6 +28,7 @@ tracing-subscriber = { workspace = true, features = ["fmt"] } tracing.workspace = true [package.metadata.docs.rs] +features = ["wasmer/cranelift"] rustc-args = ["--cfg", "docsrs"] [features] From 308b47bb10a5a1b773244f00617014b456fb1d12 Mon Sep 17 00:00:00 2001 From: Martin Liska Date: Wed, 26 Nov 2025 14:26:44 +0100 Subject: [PATCH 4/4] fix docs build --- lib/wai-bindgen-wasmer/Cargo.toml | 2 +- lib/wasi-types/Cargo.toml | 2 +- lib/wasix/Cargo.toml | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/wai-bindgen-wasmer/Cargo.toml b/lib/wai-bindgen-wasmer/Cargo.toml index 2d3f8f69e0f..694d2a36392 100644 --- a/lib/wai-bindgen-wasmer/Cargo.toml +++ b/lib/wai-bindgen-wasmer/Cargo.toml @@ -42,5 +42,5 @@ singlepass = ["wasmer/singlepass"] llvm = ["wasmer/llvm"] [package.metadata.docs.rs] -features = ["wasmer/sys", "wasmer/compiler"] +features = ["wasmer/sys", "wasmer/cranelift"] rustc-args = ["--cfg", "docsrs"] diff --git a/lib/wasi-types/Cargo.toml b/lib/wasi-types/Cargo.toml index fcdd175f719..f05230b411c 100644 --- a/lib/wasi-types/Cargo.toml +++ b/lib/wasi-types/Cargo.toml @@ -41,5 +41,5 @@ version = "1.3.0" enable-serde = ["serde", "wasmer-types/serde"] [package.metadata.docs.rs] -features = ["wasmer/sys", "wasmer/compiler"] +features = ["wasmer/sys", "wasmer/cranelift"] rustc-args = ["--cfg", "docsrs"] diff --git a/lib/wasix/Cargo.toml b/lib/wasix/Cargo.toml index ac8254cd104..ae2c4466f31 100644 --- a/lib/wasix/Cargo.toml +++ b/lib/wasix/Cargo.toml @@ -264,7 +264,7 @@ enable-serde = [ [package.metadata.docs.rs] features = [ "wasmer/sys", - "wasmer/compiler", + "wasmer/cranelift", "webc_runner_rt_wcgi", "webc_runner_rt_dcgi", "webc_runner_rt_dproxy",