Skip to content

Commit

Permalink
example: nix cross compiling
Browse files Browse the repository at this point in the history
  • Loading branch information
rickvanprim committed Nov 16, 2023
1 parent 6f9489a commit 27e716e
Show file tree
Hide file tree
Showing 65 changed files with 6,847 additions and 0 deletions.
6 changes: 6 additions & 0 deletions .bazelci/presubmit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -614,6 +614,12 @@ tasks:
# working_directory: examples/zig_cross_compiling
# build_targets:
# - "//..."
nix_cross_compiling:
name: Nix cross compiling test
platform: nixos
working_directory: examples/nix_cross_compiling
build_targets:
- "//:nix_cross_compiling"

buildifier:
version: latest
Expand Down
1 change: 1 addition & 0 deletions .envrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
use flake
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/.envrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
use flake
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-*
167 changes: 167 additions & 0 deletions examples/nix_cross_compiling/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
load("@aspect_bazel_lib//lib:transitions.bzl", "platform_transition_binary")

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

# Explicit filegroup as globbed `//...` will skip targets without an error if
# `cargo-bazel` doesn't generate a target for a given platform.
filegroup(
name = "nix_cross_compiling",
srcs = [
# "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/>
99 changes: 99 additions & 0 deletions examples/nix_cross_compiling/WORKSPACE.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")

# Aspect Bazel Lib
http_archive(
name = "aspect_bazel_lib",
sha256 = "97fa63d95cc9af006c4c7b2123ddd2a91fb8d273012f17648e6423bae2c69470",
strip_prefix = "bazel-lib-1.30.2",
url = "https://github.com/aspect-build/bazel-lib/releases/download/v1.30.2/bazel-lib-v1.30.2.tar.gz",
)

load("@aspect_bazel_lib//lib:repositories.bzl", "aspect_bazel_lib_dependencies")

aspect_bazel_lib_dependencies()

# Nix
http_archive(
name = "io_tweag_rules_nixpkgs",
sha256 = "657db2e70d933f252735a5a5c9f532c4cd5e77018b9d7256bc9c7b80a01e96b5",
strip_prefix = "rules_nixpkgs-816e913f41d161659cbd1e49ff77694853398a3a",
urls = ["https://github.com/tweag/rules_nixpkgs/archive/816e913f41d161659cbd1e49ff77694853398a3a.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 27e716e

Please sign in to comment.