Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions cargo-near-build/src/env_keys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,13 @@ pub(crate) const CARGO_NEAR_VERSION: &str = "CARGO_NEAR_VERSION";
#[cfg(feature = "build_internal")]
pub(crate) const CARGO_NEAR_ABI_SCHEMA_VERSION: &str = "CARGO_NEAR_ABI_SCHEMA_VERSION";

/// Target-scoped C compiler flags for wasm32, respected by the `cc` crate.
/// The `cc` crate checks `CFLAGS_<target>` before `CFLAGS`, allowing
/// target-specific flags without affecting host-side C compilation.
pub const CFLAGS_WASM32_ENV: &str = "CFLAGS_wasm32_unknown_unknown";
/// Target-scoped C++ compiler flags for wasm32, respected by the `cc` crate.
pub const CXXFLAGS_WASM32_ENV: &str = "CXXFLAGS_wasm32_unknown_unknown";

pub const COLOR_PREFERENCE_NO_COLOR: &str = "NO_COLOR";

use std::collections::HashMap;
Expand Down
22 changes: 21 additions & 1 deletion cargo-near-build/src/near/build/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,27 @@ pub fn run(args: Opts) -> eyre::Result<CompilationArtifact> {
let abi_path_env = buildtime_env::AbiPath::new(args.no_embed_abi, &min_abi_path);

let build_env = {
let mut build_env = vec![(env_keys::RUSTFLAGS, "-C link-arg=-s")];
let mut build_env = vec![
(env_keys::RUSTFLAGS, "-C link-arg=-s"),
// Prevent C/C++ dependencies (compiled via the `cc` crate) from emitting
// bulk-memory wasm instructions (`memory.fill`/`memory.copy`).
// LLVM 21+ enables `bulk-memory` and `bulk-memory-opt` by default for
// wasm32, but NEAR's VM rejects these instructions.
// Both flags are needed: `-mno-bulk-memory` alone is insufficient because
// LLVM 21 introduced a separate `bulk-memory-opt` feature that it doesn't
// cover. Other post-MVP features (sign-ext, mutable-globals) are left
// enabled as NearVM supports them since protocol 62.
// Target-scoped env vars ensure only wasm32 C compilation is affected,
// not any host-side C code compiled during the build.
(
env_keys::CFLAGS_WASM32_ENV,
"-mno-bulk-memory -mno-bulk-memory-opt",
),
(
env_keys::CXXFLAGS_WASM32_ENV,
"-mno-bulk-memory -mno-bulk-memory-opt",
),
];
build_env.extend(
args.env
.iter()
Expand Down
Loading