Skip to content

Commit

Permalink
fix: rustc_compile_action emits CcInfo for wasm32 targets (#1979)
Browse files Browse the repository at this point in the history
This change allows `cc_binary()`, `cc_library()`, etc. targets to depend
on `rust_libary()`, `rust_static_library()`, etc. targets when targeting
`wasm32`.
  • Loading branch information
rickvanprim authored Dec 8, 2023
1 parent 6874b8d commit 0a4c4df
Show file tree
Hide file tree
Showing 73 changed files with 6,997 additions and 2 deletions.
15 changes: 15 additions & 0 deletions .bazelci/presubmit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -621,6 +621,21 @@ tasks:
# working_directory: examples/zig_cross_compiling
# build_targets:
# - "//..."
nix_cross_compiling:
name: Nix cross compiling test
platform: ubuntu2204
working_directory: examples/nix_cross_compiling
setup:
- curl -L https://nixos.org/nix/install | bash
- sudo cp ~/.nix-profile/bin/nix* /usr/local/bin/
- sudo mkdir /etc/nix
- echo "experimental-features = nix-command flakes" | sudo tee --append /etc/nix/nix.conf
build_targets:
# Root `build_test` target is called directly to catch missing platforms
# that would otherwise be quietly skipped due to `target_compatible_with`.
- "//:nix_cross_compiling"
test_targets:
- "//..."

buildifier:
version: latest
Expand Down
10 changes: 10 additions & 0 deletions .envrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# `nix-direnv` to provide developer tools to terminals and VSCode when working
# in the repo.
#
# See:
# - https://github.com/nix-community/nix-direnv
# - https://marketplace.visualstudio.com/items?itemName=mkhl.direnv

# Use `path:` syntax to avoid copying the entire repo to the Nix Store.
nix_direnv_watch_file ./nix/flake.nix
use flake path:./nix
61 changes: 61 additions & 0 deletions crate_universe/flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 26 additions & 0 deletions crate_universe/flake.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable";

flake-utils.url = "github:numtide/flake-utils";
};

outputs = { self, nixpkgs, flake-utils, ... }:
flake-utils.lib.eachDefaultSystem (system:
let
pkgs = import nixpkgs { inherit system; };
in
{
packages.cargo-bazel = pkgs.rustPlatform.buildRustPackage {
pname = "cargo-bazel";
version = (builtins.fromTOML (builtins.readFile ./Cargo.toml)).package.version;

src = ./.;
cargoLock.lockFile = ./Cargo.lock;

# Tests cannot be run via Cargo due to the dependency on the
# Bazel `runfiles` crate.
doCheck = false;
};
});
}
1 change: 1 addition & 0 deletions examples/.bazelignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ crate_universe
crate_universe_unnamed
ios
ios_build
nix_cross_compiling
zig_cross_compiling
15 changes: 15 additions & 0 deletions examples/nix_cross_compiling/.bazelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Sensible Defaults
build --sandbox_default_allow_network=false
build --incompatible_strict_action_env
build --action_env=BAZEL_DO_NOT_DETECT_CPP_TOOLCHAIN=1
build --incompatible_enable_cc_toolchain_resolution=true

# Require Platform Transitions
## This works by setting the targte platform to an invalid platform
## and each `x_binary()` and `x_library()` rule unfortunately needs
## to tag itself with `platform_missing` to get excluded from glob
## builds like `build //...` but still have a way to include them
## by removing the filter line for things like Rust Analyzer.
build --host_platform=//bazel/platforms:host
build --platforms=//bazel/platforms:missing
build --build_tag_filters=-platform_missing
1 change: 1 addition & 0 deletions examples/nix_cross_compiling/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/bazel-*
150 changes: 150 additions & 0 deletions examples/nix_cross_compiling/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
load("@bazel_skylib//rules:build_test.bzl", "build_test")
load("//bazel:transitions.bzl", "platform_transition_binary")

# Disabled targets need the user to supply a sysroot in `flake.nix` first.

build_test(
name = "nix_cross_compiling",
targets = [
# ":cc_binary_aarch64-apple-darwin",
# ":cc_binary_aarch64-apple-ios",
":cc_binary_aarch64-linux-android",
":cc_binary_aarch64-unknown-linux-gnu",
":cc_binary_wasm32-unknown-unknown",
":cc_binary_wasm32-wasi",
# ":cc_binary_x86_64-apple-darwin",
# ":cc_binary_x86_64-pc-windows-msvc",
":cc_binary_x86_64-unknown-linux-gnu",
":cc_binary_x86_64-unknown-nixos-gnu",
# ":rust_binary_aarch64-apple-darwin",
# ":rust_binary_aarch64-apple-ios",
":rust_binary_aarch64-linux-android",
":rust_binary_aarch64-unknown-linux-gnu",
":rust_binary_wasm32-unknown-unknown",
":rust_binary_wasm32-wasi",
# ":rust_binary_x86_64-apple-darwin",
# ":rust_binary_x86_64-pc-windows-msvc",
":rust_binary_x86_64-unknown-linux-gnu",
":rust_binary_x86_64-unknown-nixos-gnu",
],
)

# platform_transition_binary(
# name = "cc_binary_aarch64-apple-darwin",
# binary = "//cc_binary",
# target_platform = "//bazel/platforms:aarch64-apple-darwin",
# )

# platform_transition_binary(
# name = "cc_binary_aarch64-apple-ios",
# binary = "//cc_binary",
# target_platform = "//bazel/platforms:aarch64-apple-ios",
# )

platform_transition_binary(
name = "cc_binary_aarch64-linux-android",
binary = "//cc_binary",
target_platform = "//bazel/platforms:aarch64-linux-android",
)

platform_transition_binary(
name = "cc_binary_aarch64-unknown-linux-gnu",
binary = "//cc_binary",
target_platform = "//bazel/platforms:aarch64-unknown-linux-gnu",
)

platform_transition_binary(
name = "cc_binary_wasm32-unknown-unknown",
binary = "//cc_binary",
target_platform = "//bazel/platforms:wasm32-unknown-unknown",
)

platform_transition_binary(
name = "cc_binary_wasm32-wasi",
binary = "//cc_binary",
target_platform = "//bazel/platforms:wasm32-wasi",
)

# platform_transition_binary(
# name = "cc_binary_x86_64-apple-darwin",
# binary = "//cc_binary",
# target_platform = "//bazel/platforms:x86_64-apple-darwin",
# )

# platform_transition_binary(
# name = "cc_binary_x86_64-pc-windows-msvc",
# binary = "//cc_binary",
# target_platform = "//bazel/platforms:x86_64-pc-windows-msvc",
# )

platform_transition_binary(
name = "cc_binary_x86_64-unknown-linux-gnu",
binary = "//cc_binary",
target_platform = "//bazel/platforms:x86_64-unknown-linux-gnu",
)

platform_transition_binary(
name = "cc_binary_x86_64-unknown-nixos-gnu",
binary = "//cc_binary",
target_platform = "//bazel/platforms:x86_64-unknown-nixos-gnu",
)

# platform_transition_binary(
# name = "rust_binary_aarch64-apple-darwin",
# binary = "//rust_binary",
# target_platform = "//bazel/platforms:aarch64-apple-darwin",
# )

# platform_transition_binary(
# name = "rust_binary_aarch64-apple-ios",
# binary = "//rust_binary",
# target_platform = "//bazel/platforms:aarch64-apple-ios",
# )

platform_transition_binary(
name = "rust_binary_aarch64-linux-android",
binary = "//rust_binary",
target_platform = "//bazel/platforms:aarch64-linux-android",
)

platform_transition_binary(
name = "rust_binary_aarch64-unknown-linux-gnu",
binary = "//rust_binary",
target_platform = "//bazel/platforms:aarch64-unknown-linux-gnu",
)

platform_transition_binary(
name = "rust_binary_wasm32-unknown-unknown",
binary = "//rust_binary",
target_platform = "//bazel/platforms:wasm32-unknown-unknown",
)

platform_transition_binary(
name = "rust_binary_wasm32-wasi",
binary = "//rust_binary",
target_platform = "//bazel/platforms:wasm32-wasi",
)

# platform_transition_binary(
# name = "rust_binary_x86_64-apple-darwin",
# binary = "//rust_binary",
# target_platform = "//bazel/platforms:x86_64-apple-darwin",
# )

# platform_transition_binary(
# name = "rust_binary_x86_64-pc-windows-msvc",
# binary = "//rust_binary",
# target_platform = "//bazel/platforms:x86_64-pc-windows-msvc",
# )

platform_transition_binary(
name = "rust_binary_x86_64-unknown-linux-gnu",
binary = "//rust_binary",
target_platform = "//bazel/platforms:x86_64-unknown-linux-gnu",
)

platform_transition_binary(
name = "rust_binary_x86_64-unknown-nixos-gnu",
binary = "//rust_binary",
target_platform = "//bazel/platforms:x86_64-unknown-nixos-gnu",
)
3 changes: 3 additions & 0 deletions examples/nix_cross_compiling/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Nix + Bazel Cross Compiling

Blog Post: <https://blog.consumingchaos.com/posts/nix-bazel-cross-compiling/>
87 changes: 87 additions & 0 deletions examples/nix_cross_compiling/WORKSPACE.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")

# Nix
http_archive(
name = "io_tweag_rules_nixpkgs",
sha256 = "54946958c311f48c17c9b2e70683b621fec135258b75173f3900f901d52d8115",
strip_prefix = "rules_nixpkgs-2b4702c8a0d1d7ea474ea0913344e8add2759f9c",
urls = ["https://github.com/tweag/rules_nixpkgs/archive/2b4702c8a0d1d7ea474ea0913344e8add2759f9c.tar.gz"],
)

load("@io_tweag_rules_nixpkgs//nixpkgs:repositories.bzl", "rules_nixpkgs_dependencies")

rules_nixpkgs_dependencies()

load("//bazel:nix_repositories.bzl", "nix_repositories")

nix_repositories()

# Rust
local_repository(
name = "rules_rust",
path = "../..",
)

load("@rules_rust//rust:repositories.bzl", "rules_rust_dependencies")

rules_rust_dependencies()

load("@rules_rust//crate_universe:defs.bzl", "crates_repository", "render_config", "splicing_config")
load("//bazel/cargo:crates_repository.bzl", CARGO_ANNOTATIONS = "ANNOTATIONS", CARGO_PACKAGES = "PACKAGES")

crates_repository(
name = "crate_index",
annotations = CARGO_ANNOTATIONS,
cargo_lockfile = "//bazel/cargo:Cargo.lock",
generate_build_scripts = False,
generator = "@cargo-bazel//:bin/cargo-bazel",
lockfile = "//bazel/cargo:cargo-bazel-lock.json",
packages = CARGO_PACKAGES,
render_config = render_config(
platforms_template = "@//bazel/cargo/platforms:{triple}",
),
rust_toolchain_cargo_template = "@nix_rust//:bin/{tool}",
rust_toolchain_rustc_template = "@nix_rust//:bin/{tool}",
splicing_config = splicing_config(
resolver_version = "2",
),
supported_platform_triples = [
"aarch64-apple-darwin",
"aarch64-apple-ios",
"aarch64-linux-android",
"aarch64-unknown-linux-gnu",
"wasm32-unknown-unknown",
"wasm32-wasi",
"x86_64-apple-darwin",
"x86_64-pc-windows-msvc",
"x86_64-unknown-linux-gnu",
],
)

load("@crate_index//:defs.bzl", "crate_repositories")

crate_repositories()

# Toolchains
register_toolchains(
"//bazel/toolchains/cc/aarch64-apple-darwin:toolchain",
"//bazel/toolchains/cc/aarch64-apple-ios:toolchain",
"//bazel/toolchains/cc/aarch64-linux-android:toolchain",
"//bazel/toolchains/cc/aarch64-unknown-linux-gnu:toolchain",
"//bazel/toolchains/cc/wasm32-unknown-unknown:toolchain",
"//bazel/toolchains/cc/wasm32-wasi:toolchain",
"//bazel/toolchains/cc/x86_64-apple-darwin:toolchain",
"//bazel/toolchains/cc/x86_64-pc-windows-msvc:toolchain",
"//bazel/toolchains/cc/x86_64-unknown-linux-gnu:toolchain",
"//bazel/toolchains/cc/x86_64-unknown-nixos-gnu:toolchain",
"//bazel/toolchains/rust/aarch64-apple-darwin:toolchain",
"//bazel/toolchains/rust/aarch64-apple-ios:toolchain",
"//bazel/toolchains/rust/aarch64-linux-android:toolchain",
"//bazel/toolchains/rust/aarch64-unknown-linux-gnu:toolchain",
"//bazel/toolchains/rust/wasm32-unknown-unknown:toolchain",
"//bazel/toolchains/rust/wasm32-wasi:toolchain",
"//bazel/toolchains/rust/x86_64-apple-darwin:toolchain",
"//bazel/toolchains/rust/x86_64-pc-windows-msvc:toolchain",
"//bazel/toolchains/rust/x86_64-unknown-linux-gnu:toolchain",
"//bazel/toolchains/rust/x86_64-unknown-nixos-gnu:toolchain",
)
Empty file.
Empty file.
Loading

0 comments on commit 0a4c4df

Please sign in to comment.