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 Jun 16, 2023
1 parent 8560aa9 commit be793fd
Show file tree
Hide file tree
Showing 51 changed files with 3,903 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 @@ -587,6 +587,12 @@ tasks:
# working_directory: examples/zig_cross_compiling
# build_targets:
# - "//..."
nix_cross_compiling:
name: Nix cross compiling test
platform: ubuntu2004
working_directory: examples/nix_cross_compiling
build_targets:
- "//..."

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

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

"""
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 Crosspiling

Blog Post: <https://consumingchaos.github.io/posts/nix-bazel-crosspiling/>
64 changes: 64 additions & 0 deletions examples/nix_cross_compiling/WORKSPACE.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
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()

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

# Nix Toolchains
load("//bazel:nix_repositories.bzl", "nix_repositories")

nix_repositories()

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",
)

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

rules_rust_dependencies()
Empty file.
Loading

0 comments on commit be793fd

Please sign in to comment.