Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

wip: allow building of wasm targets #496

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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
10 changes: 9 additions & 1 deletion examples/toolchains/rust/BUILD
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
load("@rules_rust//rust:defs.bzl", "rust_binary")
load("@rules_rust//wasm_bindgen:defs.bzl", "rust_wasm_bindgen")
load("@crate_index//:defs.bzl", "all_crate_deps")

exports_files([
Expand All @@ -10,5 +11,12 @@ exports_files([
rust_binary(
name = "hello",
srcs = ["hello.rs"],
deps = all_crate_deps(normal = True),
deps = [], # all_crate_deps(normal = True),
edition = "2021",
)

rust_wasm_bindgen(
name = "wasm",
target = "web",
wasm_file = ":hello",
)
17 changes: 16 additions & 1 deletion examples/toolchains/rust/WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ nixpkgs_cc_configure(
],
)

load("@io_tweag_rules_nixpkgs//nixpkgs:toolchains/rust.bzl", "nixpkgs_rust_configure")
load("@io_tweag_rules_nixpkgs//nixpkgs:toolchains/rust.bzl", "nixpkgs_rust_configure", "nixpkgs_rust_wasm_configure")

nixpkgs_rust_configure(
repository = "@nixpkgs",
Expand All @@ -53,6 +53,14 @@ nixpkgs_rust_configure(
],
)

nixpkgs_rust_wasm_configure(
repository = "@nixpkgs",
name = "nix_wasm_rust",
nix_file_deps = [
"//:nixpkgs.json"
],
)

nixpkgs_package(
name = "openssl-static",
repository = "@nixpkgs",
Expand All @@ -66,6 +74,13 @@ load("@rules_rust//rust:repositories.bzl", "rust_repositories")

rust_repositories()

load("@rules_rust//wasm_bindgen:repositories.bzl", "rust_wasm_bindgen_dependencies", "rust_wasm_bindgen_register_toolchains")

rust_wasm_bindgen_dependencies()

rust_wasm_bindgen_register_toolchains()


# crate_universe as a way of governing deps
load("@rules_rust//crate_universe:repositories.bzl", "crate_universe_dependencies")

Expand Down
9 changes: 5 additions & 4 deletions examples/toolchains/rust/hello.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
fn main() {
println!(
"Hello world from {:?}",
openssl::version::version()
);
println!(
"Hello world from {:?}",
String::from("wasm??"),
// openssl::version::version()
);
}
3 changes: 3 additions & 0 deletions nixpkgs/toolchains/rust.bzl
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# alias to Bazel module `toolchains/go`

load("@rules_nixpkgs_rust//:rust.bzl", _nixpkgs_rust_configure = "nixpkgs_rust_configure")
load("@rules_nixpkgs_rust//:wasm.bzl", _nixpkgs_rust_wasm_configure = "nixpkgs_rust_wasm_configure")

nixpkgs_rust_configure = _nixpkgs_rust_configure
nixpkgs_rust_wasm_configure = _nixpkgs_rust_wasm_configure

2 changes: 1 addition & 1 deletion toolchains/rust/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ filegroup(

bzl_library(
name = "rust",
srcs = ["//:rust.bzl"],
srcs = ["//:rust.bzl", "//:wasm.bzl"],
deps = [
"@rules_nixpkgs_core//:nixpkgs",
],
Expand Down
219 changes: 219 additions & 0 deletions toolchains/rust/wasm.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,219 @@
"""<!-- Edit the docstring in `toolchains/rust/rust.bzl` and run `bazel run //docs:update-README.md` to change this repository's `README.md`. -->

Rules for importing a Rust toolchain from Nixpkgs.

# Rules

* [nixpkgs_rust_wasm_configure](#nixpkgs_rust_configure)
"""

load("@rules_nixpkgs_core//:nixpkgs.bzl", "nixpkgs_package")
load("@rules_nixpkgs_core//:util.bzl", "ensure_constraints")

# Adapted from rules_rust toolchain BUILD:
# https://github.com/bazelbuild/rules_rust/blob/fd436df9e2d4ac1b234ca5e969e34a4cb5891910/rust/private/repository_utils.bzl#L17-L46
# Nix generation is used to dynamically compute both Linux and Darwin environments
_rust_nix_contents = """\
let
pkgs = import <nixpkgs> {{
config = {{}};
overrides = [];
}};
rust = pkgs.rust;
os = rust.toTargetOs pkgs.stdenv.targetPlatform;
build-triple = rust.toRustTargetSpec pkgs.stdenv.buildPlatform;
target-triple = rust.toRustTargetSpec pkgs.stdenv.targetPlatform;
rustc-wasm32 = if os == "macos" then
# rustc on MacOS expects the `strip` binary in PATH, create a wrapper adding the bin
# folder from `binutils` to the PATH
pkgs.symlinkJoin
{{
name = "rustc-wasm32";
paths = [ pkgs.rustc-wasm32 ];
nativeBuildInputs = [ pkgs.makeWrapper ];
postBuild = ''
wrapProgram $out/bin/rustc --suffix PATH : ${{pkgs.binutils}}/bin
'';
}}
else pkgs.rustc-wasm32;
rustdoc = pkgs.stdenv.mkDerivation {{
name = "rustdoc";
src = pkgs.rustc;
phases = ["unpackPhase"];
unpackPhase = ''
mkdir -p $out/bin
cp $src/bin/rustdoc $out/bin/
'';
}};
binary-ext = "";
staticlib-ext = ".a";
dylib-ext = if os == "macos" then ".dylib" else ".so";
in
pkgs.buildEnv {{
extraOutputsToInstall = ["out" "bin" "lib"];
name = "bazel-rust-wasm-toolchain";
paths = [ rustc-wasm32 rustdoc pkgs.rustfmt pkgs.cargo pkgs.clippy ];
postBuild = ''
cat <<EOF > $out/BUILD
filegroup(
name = "rustc",
srcs = ["bin/rustc"],
visibility = ["//visibility:public"],
)

filegroup(
name = "rustfmt",
srcs = ["bin/rustfmt"],
visibility = ["//visibility:public"],
)

filegroup(
name = "cargo",
srcs = ["bin/cargo"],
visibility = ["//visibility:public"],
)

filegroup(
name = "clippy_driver",
srcs = ["bin/clippy-driver"],
visibility = ["//visibility:public"],
)

filegroup(
name = "rustc_lib",
srcs = glob(
[
"bin/*.so",
"lib/*.so",
"lib/rustlib/*/codegen-backends/*.so",
"lib/rustlib/*/codegen-backends/*.dylib",
"lib/rustlib/*/bin/rust-lld",
"lib/rustlib/*/lib/*.so",
"lib/rustlib/*/lib/*.dylib",
],
allow_empty = True,
),
visibility = ["//visibility:public"],
)

load("@rules_rust//rust:toolchain.bzl", "rust_stdlib_filegroup")
rust_stdlib_filegroup(
name = "rust_std",
srcs = glob(
[
"lib/rustlib/*/lib/*.rlib",
"lib/rustlib/*/lib/*.so",
"lib/rustlib/*/lib/*.dylib",
"lib/rustlib/*/lib/*.a",
"lib/rustlib/*/lib/self-contained/**",
],
# Some patterns (e.g. `lib/*.a`) don't match anything, see https://github.com/bazelbuild/rules_rust/pull/245
allow_empty = True,
),
visibility = ["//visibility:public"],
)

filegroup(
name = "rust_doc",
srcs = ["bin/rustdoc"],
visibility = ["//visibility:public"],
)

load('@rules_rust//rust:toolchain.bzl', 'rust_toolchain')
rust_toolchain(
name = "rust_wasm_nix_impl",
rust_doc = ":rust_doc",
rust_std = ":rust_std",
rustc = ":rustc",
rustfmt = ":rustfmt",
cargo = ":cargo",
clippy_driver = ":clippy_driver",
rustc_lib = ":rustc_lib",
binary_ext = "${{binary-ext}}",
staticlib_ext = "${{staticlib-ext}}",
dylib_ext = "${{dylib-ext}}",
exec_triple = "${{build-triple}}",
default_edition = "{default_edition}",
stdlib_linkflags = {stdlib_linkflags},
visibility = ["//visibility:public"],
target_triple = "wasm32-unknown-unknown",
)

EOF
'';
}}
"""

_rust_nix_toolchain = """
toolchain(
name = "rust_wasm_nix",
toolchain = "@{toolchain_repo}//:rust_wasm_nix_impl",
toolchain_type = "@rules_rust//rust:toolchain",
exec_compatible_with = {exec_constraints},
target_compatible_with = [
"@platforms//cpu:wasm32",
"@platforms//os:none",
],
)
"""

def _nixpkgs_rust_toolchain_impl(repository_ctx):
exec_constraints, target_constraints = ensure_constraints(repository_ctx)
repository_ctx.file(
"BUILD.bazel",
executable = False,
content = _rust_nix_toolchain.format(
toolchain_repo = repository_ctx.attr.toolchain_repo,
exec_constraints = exec_constraints,
target_constraints = target_constraints,
),
)

_nixpkgs_rust_toolchain = repository_rule(
_nixpkgs_rust_toolchain_impl,
attrs = {
"toolchain_repo": attr.string(),
"exec_constraints": attr.string_list(),
"target_constraints": attr.string_list(),
},
)

def nixpkgs_rust_wasm_configure(
name = "nixpkgs_rust",
default_edition = "2018",
repository = None,
repositories = {},
nix_file = None,
nix_file_deps = None,
nix_file_content = None,
nixopts = [],
fail_not_supported = True,
quiet = False,
exec_constraints = None,
target_constraints = None,
register = True):
if not nix_file and not nix_file_content:
nix_file_content = _rust_nix_contents.format(
default_edition = default_edition,
stdlib_linkflags = '["-lpthread", "-ldl"]',
)

nixpkgs_package(
name = name,
repository = repository,
repositories = repositories,
nix_file = nix_file,
nix_file_deps = nix_file_deps,
nix_file_content = nix_file_content,
nixopts = nixopts,
fail_not_supported = fail_not_supported,
quiet = quiet,
)
_nixpkgs_rust_toolchain(
name = name + "_toolchain",
toolchain_repo = name,
exec_constraints = exec_constraints,
target_constraints = target_constraints,
)
if register:
native.register_toolchains("@{}_toolchain//:rust_wasm_nix".format(name))