Skip to content

Commit

Permalink
Support compilation-only build by adding a runtime feature (#7766)
Browse files Browse the repository at this point in the history
* Add `runtime` feature to `wasmtime` crate

This feature can be disabled to build `wasmtime` only for compilation.
This can be useful when cross-compiling, especially on a target that
can't run wasmtime itself (e.g. `wasm32`).

* prtest:full

* don't round pages without runtime feature

* fix async assertions

* move profiling into runtime

* enable runtime for wasmtime-wasi

* enable runtime for c-api

* fix build_artifacts in non-cache case

* fix miri extensions

* enable runtime for wast

* enable runtime for explorer

* support cranelift all-arch on wasm32

* add doc links for `WeakEngine`

* simplify lib runtime cfgs

* move limits and resources to runtime

* move stack to runtime

* move coredump and debug to runtime

* add runtime to coredump and async features

* add wasm32 build job

* combine engine modules

* single compile mod

* remove allow for macro paths

* add comments
  • Loading branch information
adambratschikaye authored Jan 29, 2024
1 parent 04adffd commit d424200
Show file tree
Hide file tree
Showing 74 changed files with 1,193 additions and 850 deletions.
21 changes: 21 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -570,6 +570,27 @@ jobs:
GH_TOKEN: ${{ github.token }}


build-wasmtime-target-wasm32:
name: Build wasmtime-target-wasm32
needs: determine
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
submodules: true
- run: rustup update stable && rustup default stable
- run: rustup target add wasm32-wasi wasm32-unknown-unknown
- run: cargo build -p wasmtime --target wasm32-wasi --no-default-features --features cranelift,all-arch
env:
VERSION: ${{ github.sha }}

# common logic to cancel the entire run if this job fails
- run: gh run cancel ${{ github.run_id }}
if: failure() && github.event_name != 'pull_request'
env:
GH_TOKEN: ${{ github.token }}


bench:
needs: determine
if: needs.determine.outputs.run-full
Expand Down
7 changes: 5 additions & 2 deletions cranelift/codegen/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,19 @@ fn main() {
let out_dir = env::var("OUT_DIR").expect("The OUT_DIR environment variable must be set");
let target_triple = env::var("TARGET").expect("The TARGET environment variable must be set");

let all_arch = env::var("CARGO_FEATURE_ALL_ARCH").is_ok();

let mut isas = meta::isa::Isa::all()
.iter()
.cloned()
.filter(|isa| {
let env_key = format!("CARGO_FEATURE_{}", isa.to_string().to_uppercase());
env::var(env_key).is_ok()
all_arch || env::var(env_key).is_ok()
})
.collect::<Vec<_>>();

let host_isa = env::var("CARGO_FEATURE_HOST_ARCH").is_ok();
// Don't require host isa if under 'all-arch' feature.
let host_isa = env::var("CARGO_FEATURE_HOST_ARCH").is_ok() && !all_arch;

if isas.is_empty() || host_isa {
// Try to match native target.
Expand Down
4 changes: 4 additions & 0 deletions cranelift/native/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,10 @@ pub fn infer_native_flags(isa_builder: &mut dyn Configurable) -> Result<(), &'st
// the cpuinfo interface, so we can't rely on it being present for now.
let _ = riscv::cpuinfo_detect(isa_builder);
}

// On all other architectures (e.g. wasm32) we won't infer any native flags,
// but still need to use the `isa_builder` to avoid compiler warnings.
let _ = isa_builder;
Ok(())
}

Expand Down
2 changes: 1 addition & 1 deletion crates/c-api/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ doctest = false
env_logger = { workspace = true, optional = true }
anyhow = { workspace = true }
once_cell = { workspace = true }
wasmtime = { workspace = true, features = ['cranelift'] }
wasmtime = { workspace = true, features = ['cranelift', 'runtime'] }
wasmtime-c-api-macros = { path = "macros" }
log = { workspace = true }
tracing = { workspace = true }
Expand Down
9 changes: 5 additions & 4 deletions crates/cranelift-shared/src/isa_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,22 +20,23 @@ pub struct IsaBuilder<T> {

impl<T> IsaBuilder<T> {
/// Create a new ISA builder with the given lookup function.
pub fn new(lookup: fn(Triple) -> Result<Builder<T>>) -> Self {
pub fn new(triple: Option<Triple>, lookup: fn(Triple) -> Result<Builder<T>>) -> Result<Self> {
let mut flags = settings::builder();

// We don't use probestack as a stack limit mechanism
flags
.set("enable_probestack", "false")
.expect("should be valid flag");

let mut isa_flags = lookup(Triple::host()).expect("host machine is not a supported target");
let triple = triple.unwrap_or_else(Triple::host);
let mut isa_flags = lookup(triple)?;
cranelift_native::infer_native_flags(&mut isa_flags).unwrap();

Self {
Ok(Self {
shared_flags: flags,
inner: isa_flags,
lookup,
}
})
}

pub fn triple(&self) -> &target_lexicon::Triple {
Expand Down
1 change: 1 addition & 0 deletions crates/cranelift/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ wasmtime-versioned-export-macros = { workspace = true }

[features]
all-arch = ["cranelift-codegen/all-arch"]
host-arch = ["cranelift-codegen/host-arch"]
component-model = ["wasmtime-environ/component-model"]
incremental-cache = ["cranelift-codegen/incremental-cache"]
wmemcheck = []
9 changes: 5 additions & 4 deletions crates/cranelift/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use cranelift_codegen::{
use std::fmt;
use std::path;
use std::sync::Arc;
use target_lexicon::Triple;
use wasmtime_cranelift_shared::isa_builder::IsaBuilder;
use wasmtime_environ::{CacheStore, CompilerBuilder, Setting, Tunables};

Expand All @@ -36,15 +37,15 @@ pub struct LinkOptions {
pub force_jump_veneers: bool,
}

pub fn builder() -> Box<dyn CompilerBuilder> {
Box::new(Builder {
pub fn builder(triple: Option<Triple>) -> Result<Box<dyn CompilerBuilder>> {
Ok(Box::new(Builder {
tunables: Tunables::default(),
inner: IsaBuilder::new(|triple| isa::lookup(triple).map_err(|e| e.into())),
inner: IsaBuilder::new(triple, |triple| isa::lookup(triple).map_err(|e| e.into()))?,
linkopts: LinkOptions::default(),
cache_store: None,
clif_dir: None,
wmemcheck: false,
})
}))
}

impl CompilerBuilder for Builder {
Expand Down
2 changes: 1 addition & 1 deletion crates/explorer/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@ serde_derive = { workspace = true }
serde_json = { workspace = true }
target-lexicon = { workspace = true }
wasmprinter = { workspace = true }
wasmtime = { workspace = true, features = ["cranelift"] }
wasmtime = { workspace = true, features = ["cranelift", "runtime"] }
1 change: 1 addition & 0 deletions crates/wasi/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ exit = []
preview2 = [
'wasmtime/component-model',
'wasmtime/async',
'wasmtime/runtime',
'dep:thiserror',
'dep:tracing',
'dep:cap-std',
Expand Down
22 changes: 12 additions & 10 deletions crates/wasmtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ rustdoc-args = ["--cfg", "nightlydoc"]
features = ["component-model"]

[dependencies]
wasmtime-runtime = { workspace = true }
wasmtime-runtime = { workspace = true, optional = true }
wasmtime-environ = { workspace = true }
wasmtime-jit-debug = { workspace = true, features = [
"perf_jitdump",
Expand Down Expand Up @@ -96,6 +96,7 @@ default = [
'addr2line',
'coredump',
'debug-builtins',
'runtime',
]

# An on-by-default feature enabling runtime compilation of WebAssembly modules
Expand Down Expand Up @@ -132,18 +133,16 @@ async = [
"wasmtime-runtime/async",
"dep:async-trait",
"wasmtime-component-macro?/async",
"runtime",
]

# Enables support for the pooling instance allocation strategy
pooling-allocator = ["wasmtime-runtime/pooling-allocator"]
pooling-allocator = ["runtime", "wasmtime-runtime/pooling-allocator"]

# Enables support for all architectures in Cranelift, allowing
# cross-compilation using the `wasmtime` crate's API, notably the
# `Engine::precompile_module` function.
all-arch = [
"wasmtime-cranelift?/all-arch",
"wasmtime-winch?/all-arch",
]
all-arch = ["wasmtime-cranelift?/all-arch", "wasmtime-winch?/all-arch"]

# Enables in-progress support for the component model. Note that this feature is
# in-progress, buggy, and incomplete. This is primarily here for internal
Expand All @@ -152,21 +151,24 @@ component-model = [
"wasmtime-environ/component-model",
"wasmtime-cranelift?/component-model",
"wasmtime-winch?/component-model",
"wasmtime-runtime/component-model",
"wasmtime-runtime?/component-model",
"dep:wasmtime-component-macro",
"dep:wasmtime-component-util",
"dep:encoding_rs",
]

wmemcheck = ["wasmtime-runtime/wmemcheck", "wasmtime-cranelift?/wmemcheck"]
wmemcheck = ["wasmtime-runtime?/wmemcheck", "wasmtime-cranelift?/wmemcheck"]

# Enables support for demangling WebAssembly function names at runtime in
# errors such as backtraces.
demangle = ["wasmtime-environ/demangle"]

# Enable support for generating core dumps on traps.
coredump = ["dep:wasm-encoder"]
coredump = ["dep:wasm-encoder", "runtime"]

# Export some symbols from the final binary to assist in debugging
# Cranelift-generated code with native debuggers like GDB and LLDB.
debug-builtins = ["wasmtime-runtime/debug-builtins"]
debug-builtins = ["wasmtime-runtime?/debug-builtins"]

# Enable support for executing compiled Wasm modules.
runtime = ["dep:wasmtime-runtime"]
Loading

0 comments on commit d424200

Please sign in to comment.