Skip to content

Commit

Permalink
Add wasm32-freestanding-none zig cc toolchain
Browse files Browse the repository at this point in the history
  • Loading branch information
nmattia committed Jan 24, 2025
1 parent 8678aeb commit 9915126
Show file tree
Hide file tree
Showing 6 changed files with 120 additions and 14 deletions.
11 changes: 11 additions & 0 deletions MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ use_repo(toolchains, "zig_sdk")

register_toolchains(
"@zig_sdk//toolchain:linux_amd64_gnu.2.28",
"@zig_sdk//toolchain:wasm32-unknown-unknown",
# # if no `--platform` is specified, these toolchains will be used for
# # (linux,darwin,windows)x(amd64,arm64)
# "@zig_sdk-linux-amd64//toolchain:linux_x86_64_gnu.2.28",
Expand All @@ -44,6 +45,16 @@ register_toolchains(
# dev_dependency = True # TODO: what is this
)


archive_override(
module_name = "hermetic_cc_toolchain",
integrity = "sha384-wZ/rdjnw2vl00OndRQERXS/HcFKLmgqZG+hnbphTtWtxeNeIcWZk0dzKZByWKJsL",
patch_strip = 1,
patches = ["//bazel:rules_hermetic.patch"],
strip_prefix = "hermetic_cc_toolchain-0e98778e4e8b60c1c773199cfb340de66d62d485",
urls = ["https://github.com/uber/hermetic_cc_toolchain/archive/0e98778e4e8b60c1c773199cfb340de66d62d485.tar.gz"],
)

# Use HEAD to include this commit which is needed for preinstalled toolchains to work
# https://github.com/bazel-contrib/rules_foreign_cc/commit/d03f7ae79ddda0ad228b17048b9e2dc0efcc8e95
#
Expand Down
2 changes: 2 additions & 0 deletions WORKSPACE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@ sol_register_toolchains(

http_archive(
name = "rules_rust",
patch_args = ["-p1"],
patches = ["//bazel:rules_rust.patch"],
integrity = "sha256-8TBqrAsli3kN8BrZq8arsN8LZUFsdLTvJ/Sqsph4CmQ=",
urls = ["https://github.com/bazelbuild/rules_rust/releases/download/0.56.0/rules_rust-0.56.0.tar.gz"],
)
Expand Down
2 changes: 1 addition & 1 deletion bazel/canisters.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ load("//bazel:candid.bzl", "did_git_test")

def _wasm_rust_transition_impl(_settings, attr):
return {
"//command_line_option:platforms": "@rules_rust//rust/platform:wasm",
"//command_line_option:platforms": "@rules_rust//rust/platform:wasm32",
"@rules_rust//:extra_rustc_flags": [
# rustc allocates a default stack size of 1MiB for Wasm, which causes stack overflow on certain
# recursive workloads when compiled with 1.78.0+. Hence, we set the new stack size to 3MiB
Expand Down
21 changes: 8 additions & 13 deletions bazel/cc_rs.patch
Original file line number Diff line number Diff line change
@@ -1,22 +1,17 @@
diff --git a/src/lib.rs b/src/lib.rs
index f75b951..4c33934 100644
index f75b951..0fcd71e 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -1937,7 +1937,9 @@ impl Build {
target: &TargetInfo<'_>,
opt_level: &str,
) -> Result<(), Error> {
+
let raw_target = self.get_raw_target()?;
+
// Non-target flags
// If the flag is not conditioned on target variable, it belongs here :)
match cmd.family {
@@ -2091,6 +2093,8 @@ impl Build {
@@ -2091,6 +2091,13 @@ impl Build {
target.versioned_llvm_target(None)
};

+ let llvm_target = llvm_target.split("-").filter(|x| !x.contains("unknown")).collect::<Vec<_>>().join("-");
+ let llvm_target = match llvm_target.clone() {
+ std::borrow::Cow::Borrowed("x86_64-unknown-linux-gnu") => "x86_64-linux-gnu",
+ std::borrow::Cow::Borrowed("wasm32-unknown-unknown") => "wasm32-freestanding-none",
+ _other => &llvm_target,
+ };
+
+
// Pass `--target` with the LLVM target to properly
// configure Clang even when cross-compiling.
Expand Down
49 changes: 49 additions & 0 deletions bazel/rules_hermetic.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
diff --git a/toolchain/private/defs.bzl b/toolchain/private/defs.bzl
index 716a3a3..33f1993 100644
--- a/toolchain/private/defs.bzl
+++ b/toolchain/private/defs.bzl
@@ -49,6 +49,7 @@ def target_structs():
for glibc in _GLIBCS:
ret.append(_target_linux_gnu(gocpu, zigcpu, glibc))
ret.append(_target_wasm())
+ ret.append(_target_wasm_unknown())
return ret

def _target_macos(gocpu, zigcpu):
@@ -222,3 +223,23 @@ def _target_wasm():
ld_zig_subcmd = "wasm-ld",
artifact_name_patterns = [],
)
+
+def _target_wasm_unknown():
+ return struct(
+ gotarget = "wasm32-unknown-unknown",
+ zigtarget = "wasm32-freestanding-none",
+ includes = [
+ ] + _INCLUDE_TAIL,
+ linkopts = [],
+ dynamic_library_linkopts = [],
+ supports_dynamic_linker = False,
+ copts = [],
+ libc = "musl",
+ bazel_target_cpu = "wasm32",
+ constraint_values = [
+ "@platforms//os:none",
+ "@platforms//cpu:wasm32",
+ ],
+ ld_zig_subcmd = "wasm-ld",
+ artifact_name_patterns = [],
+ )
diff --git a/toolchain/zig-wrapper.zig b/toolchain/zig-wrapper.zig
index d1d59f9..5e2984b 100644
--- a/toolchain/zig-wrapper.zig
+++ b/toolchain/zig-wrapper.zig
@@ -283,7 +283,7 @@ fn getRunMode(self_exe: []const u8, self_base_noexe: []const u8) error{BadParent
return error.BadParent;

const got_os = it.next() orelse return error.BadParent;
- if (mem.indexOf(u8, "linux,macos,windows,wasi", got_os) == null)
+ if (mem.indexOf(u8, "linux,macos,windows,wasi,freestanding", got_os) == null)
return error.BadParent;

// ABI triple is too much of a moving target
49 changes: 49 additions & 0 deletions bazel/rules_rust.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
diff --git a/extensions/wasm_bindgen/MODULE.bazel b/extensions/wasm_bindgen/MODULE.bazel
index eba0d184..6da4e198 100644
--- a/extensions/wasm_bindgen/MODULE.bazel
+++ b/extensions/wasm_bindgen/MODULE.bazel
@@ -54,7 +54,7 @@ use_repo(

register_toolchains(
"//:default_wasm_bindgen_toolchain",
- "@rules_rust//rust/private/dummy_cc_toolchain:dummy_cc_wasm32_toolchain",
+ #"@rules_rust//rust/private/dummy_cc_toolchain:dummy_cc_wasm32_toolchain",
"@rules_rust//rust/private/dummy_cc_toolchain:dummy_cc_wasm64_toolchain",
)

diff --git a/rust/private/dummy_cc_toolchain/BUILD.bazel b/rust/private/dummy_cc_toolchain/BUILD.bazel
index df4290af..4a0dfa45 100644
--- a/rust/private/dummy_cc_toolchain/BUILD.bazel
+++ b/rust/private/dummy_cc_toolchain/BUILD.bazel
@@ -8,12 +8,12 @@ load(":dummy_cc_toolchain.bzl", "dummy_cc_config", "dummy_cc_toolchain")

dummy_cc_toolchain(name = "dummy_cc_wasm")

-toolchain(
- name = "dummy_cc_wasm32_toolchain",
- target_compatible_with = ["@platforms//cpu:wasm32"],
- toolchain = ":dummy_cc_wasm_toolchain_cc",
- toolchain_type = "@bazel_tools//tools/cpp:toolchain_type",
-)
+#toolchain(
+# name = "dummy_cc_wasm32_toolchain",
+# target_compatible_with = ["@platforms//cpu:wasm32"],
+# toolchain = ":dummy_cc_wasm_toolchain_cc",
+# toolchain_type = "@bazel_tools//tools/cpp:toolchain_type",
+#)

toolchain(
name = "dummy_cc_wasm64_toolchain",
diff --git a/rust/repositories.bzl b/rust/repositories.bzl
index 06de237d..09566384 100644
--- a/rust/repositories.bzl
+++ b/rust/repositories.bzl
@@ -1218,7 +1218,7 @@ def rust_repository_set(
# Register toolchains
if register_toolchain:
native.register_toolchains(*toolchain_labels)
- native.register_toolchains(str(Label("//rust/private/dummy_cc_toolchain:dummy_cc_wasm32_toolchain")))
+ #native.register_toolchains(str(Label("//rust/private/dummy_cc_toolchain:dummy_cc_wasm32_toolchain")))
native.register_toolchains(str(Label("//rust/private/dummy_cc_toolchain:dummy_cc_wasm64_toolchain")))

return all_toolchain_details

0 comments on commit 9915126

Please sign in to comment.