From be793fdb104b8274de013c3796200637154ae6af Mon Sep 17 00:00:00 2001 From: James Leitch Date: Fri, 16 Jun 2023 10:25:58 -0700 Subject: [PATCH] example: nix cross compiling --- .bazelci/presubmit.yml | 6 + examples/nix_cross_compiling/.bazelrc | 15 + examples/nix_cross_compiling/.envrc | 1 + examples/nix_cross_compiling/.gitignore | 1 + examples/nix_cross_compiling/BUILD.bazel | 139 +++++++++ examples/nix_cross_compiling/README.md | 3 + examples/nix_cross_compiling/WORKSPACE.bazel | 64 ++++ examples/nix_cross_compiling/bazel/BUILD | 0 .../bazel/nix_repositories.bzl | 217 +++++++++++++ .../nix_cross_compiling/bazel/platforms/BUILD | 104 +++++++ .../bazel/toolchain_rules/BUILD | 0 .../bazel/toolchain_rules/cc_tools/BUILD | 0 .../bazel/toolchain_rules/cc_tools/clang.bzl | 164 ++++++++++ .../bazel/toolchain_rules/cc_tools/ld.lld.bzl | 286 ++++++++++++++++++ .../toolchain_rules/cc_tools/ld64.lld.bzl | 282 +++++++++++++++++ .../toolchain_rules/cc_tools/lld-link.bzl | 279 +++++++++++++++++ .../toolchain_rules/cc_tools/llvm-ar.bzl | 63 ++++ .../toolchain_rules/cc_tools/llvm-strip.bzl | 30 ++ .../toolchain_rules/cc_tools/wasm-ld.bzl | 278 +++++++++++++++++ .../llvm_cc_toolchain_config.bzl | 229 ++++++++++++++ .../toolchains/cc/aarch64-apple-darwin/BUILD | 98 ++++++ .../toolchains/cc/aarch64-apple-ios/BUILD | 96 ++++++ .../toolchains/cc/aarch64-linux-android/BUILD | 111 +++++++ .../cc/aarch64-unknown-linux-gnu/BUILD | 100 ++++++ .../cc/wasm32-unknown-unknown/BUILD | 72 +++++ .../bazel/toolchains/cc/wasm32-wasi/BUILD | 83 +++++ .../toolchains/cc/x86_64-apple-darwin/BUILD | 98 ++++++ .../cc/x86_64-pc-windows-msvc/BUILD | 90 ++++++ .../cc/x86_64-unknown-linux-gnu/BUILD | 100 ++++++ .../cc/x86_64-unknown-nixos-gnu/BUILD | 96 ++++++ .../rust/aarch64-apple-darwin/BUILD | 42 +++ .../toolchains/rust/aarch64-apple-ios/BUILD | 42 +++ .../rust/aarch64-linux-android/BUILD | 35 +++ .../rust/aarch64-unknown-linux-gnu/BUILD | 35 +++ .../rust/wasm32-unknown-unknown/BUILD | 35 +++ .../bazel/toolchains/rust/wasm32-wasi/BUILD | 35 +++ .../toolchains/rust/x86_64-apple-darwin/BUILD | 42 +++ .../rust/x86_64-pc-windows-msvc/BUILD | 35 +++ .../rust/x86_64-unknown-linux-gnu/BUILD | 35 +++ .../rust/x86_64-unknown-nixos-gnu/BUILD | 35 +++ examples/nix_cross_compiling/cc_binary/BUILD | 15 + .../cc_binary/cc_binary.cc | 12 + .../cc_binary_wasm32-unknown-unknown.cc | 10 + examples/nix_cross_compiling/cc_library/BUILD | 9 + .../cc_library/cc_library.cc | 3 + examples/nix_cross_compiling/flake.lock | 159 ++++++++++ examples/nix_cross_compiling/flake.nix | 172 +++++++++++ .../nix_cross_compiling/rust_binary/BUILD | 13 + .../rust_binary/rust_binary.rs | 10 + .../nix_cross_compiling/rust_library/BUILD | 19 ++ .../rust_library/rust_library.rs | 5 + 51 files changed, 3903 insertions(+) create mode 100644 examples/nix_cross_compiling/.bazelrc create mode 100644 examples/nix_cross_compiling/.envrc create mode 100644 examples/nix_cross_compiling/.gitignore create mode 100644 examples/nix_cross_compiling/BUILD.bazel create mode 100644 examples/nix_cross_compiling/README.md create mode 100644 examples/nix_cross_compiling/WORKSPACE.bazel create mode 100644 examples/nix_cross_compiling/bazel/BUILD create mode 100644 examples/nix_cross_compiling/bazel/nix_repositories.bzl create mode 100644 examples/nix_cross_compiling/bazel/platforms/BUILD create mode 100644 examples/nix_cross_compiling/bazel/toolchain_rules/BUILD create mode 100644 examples/nix_cross_compiling/bazel/toolchain_rules/cc_tools/BUILD create mode 100644 examples/nix_cross_compiling/bazel/toolchain_rules/cc_tools/clang.bzl create mode 100644 examples/nix_cross_compiling/bazel/toolchain_rules/cc_tools/ld.lld.bzl create mode 100644 examples/nix_cross_compiling/bazel/toolchain_rules/cc_tools/ld64.lld.bzl create mode 100644 examples/nix_cross_compiling/bazel/toolchain_rules/cc_tools/lld-link.bzl create mode 100644 examples/nix_cross_compiling/bazel/toolchain_rules/cc_tools/llvm-ar.bzl create mode 100644 examples/nix_cross_compiling/bazel/toolchain_rules/cc_tools/llvm-strip.bzl create mode 100644 examples/nix_cross_compiling/bazel/toolchain_rules/cc_tools/wasm-ld.bzl create mode 100644 examples/nix_cross_compiling/bazel/toolchain_rules/llvm_cc_toolchain_config.bzl create mode 100644 examples/nix_cross_compiling/bazel/toolchains/cc/aarch64-apple-darwin/BUILD create mode 100644 examples/nix_cross_compiling/bazel/toolchains/cc/aarch64-apple-ios/BUILD create mode 100644 examples/nix_cross_compiling/bazel/toolchains/cc/aarch64-linux-android/BUILD create mode 100644 examples/nix_cross_compiling/bazel/toolchains/cc/aarch64-unknown-linux-gnu/BUILD create mode 100644 examples/nix_cross_compiling/bazel/toolchains/cc/wasm32-unknown-unknown/BUILD create mode 100644 examples/nix_cross_compiling/bazel/toolchains/cc/wasm32-wasi/BUILD create mode 100644 examples/nix_cross_compiling/bazel/toolchains/cc/x86_64-apple-darwin/BUILD create mode 100644 examples/nix_cross_compiling/bazel/toolchains/cc/x86_64-pc-windows-msvc/BUILD create mode 100644 examples/nix_cross_compiling/bazel/toolchains/cc/x86_64-unknown-linux-gnu/BUILD create mode 100644 examples/nix_cross_compiling/bazel/toolchains/cc/x86_64-unknown-nixos-gnu/BUILD create mode 100644 examples/nix_cross_compiling/bazel/toolchains/rust/aarch64-apple-darwin/BUILD create mode 100644 examples/nix_cross_compiling/bazel/toolchains/rust/aarch64-apple-ios/BUILD create mode 100644 examples/nix_cross_compiling/bazel/toolchains/rust/aarch64-linux-android/BUILD create mode 100644 examples/nix_cross_compiling/bazel/toolchains/rust/aarch64-unknown-linux-gnu/BUILD create mode 100644 examples/nix_cross_compiling/bazel/toolchains/rust/wasm32-unknown-unknown/BUILD create mode 100644 examples/nix_cross_compiling/bazel/toolchains/rust/wasm32-wasi/BUILD create mode 100644 examples/nix_cross_compiling/bazel/toolchains/rust/x86_64-apple-darwin/BUILD create mode 100644 examples/nix_cross_compiling/bazel/toolchains/rust/x86_64-pc-windows-msvc/BUILD create mode 100644 examples/nix_cross_compiling/bazel/toolchains/rust/x86_64-unknown-linux-gnu/BUILD create mode 100644 examples/nix_cross_compiling/bazel/toolchains/rust/x86_64-unknown-nixos-gnu/BUILD create mode 100644 examples/nix_cross_compiling/cc_binary/BUILD create mode 100644 examples/nix_cross_compiling/cc_binary/cc_binary.cc create mode 100644 examples/nix_cross_compiling/cc_binary/cc_binary_wasm32-unknown-unknown.cc create mode 100644 examples/nix_cross_compiling/cc_library/BUILD create mode 100644 examples/nix_cross_compiling/cc_library/cc_library.cc create mode 100644 examples/nix_cross_compiling/flake.lock create mode 100644 examples/nix_cross_compiling/flake.nix create mode 100644 examples/nix_cross_compiling/rust_binary/BUILD create mode 100644 examples/nix_cross_compiling/rust_binary/rust_binary.rs create mode 100644 examples/nix_cross_compiling/rust_library/BUILD create mode 100644 examples/nix_cross_compiling/rust_library/rust_library.rs diff --git a/.bazelci/presubmit.yml b/.bazelci/presubmit.yml index eaf1ffcf87..5385655cb0 100644 --- a/.bazelci/presubmit.yml +++ b/.bazelci/presubmit.yml @@ -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 diff --git a/examples/nix_cross_compiling/.bazelrc b/examples/nix_cross_compiling/.bazelrc new file mode 100644 index 0000000000..6686d26d11 --- /dev/null +++ b/examples/nix_cross_compiling/.bazelrc @@ -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 diff --git a/examples/nix_cross_compiling/.envrc b/examples/nix_cross_compiling/.envrc new file mode 100644 index 0000000000..3550a30f2d --- /dev/null +++ b/examples/nix_cross_compiling/.envrc @@ -0,0 +1 @@ +use flake diff --git a/examples/nix_cross_compiling/.gitignore b/examples/nix_cross_compiling/.gitignore new file mode 100644 index 0000000000..a6ef824c1f --- /dev/null +++ b/examples/nix_cross_compiling/.gitignore @@ -0,0 +1 @@ +/bazel-* diff --git a/examples/nix_cross_compiling/BUILD.bazel b/examples/nix_cross_compiling/BUILD.bazel new file mode 100644 index 0000000000..316f6ade89 --- /dev/null +++ b/examples/nix_cross_compiling/BUILD.bazel @@ -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", +) diff --git a/examples/nix_cross_compiling/README.md b/examples/nix_cross_compiling/README.md new file mode 100644 index 0000000000..8111bdc6a0 --- /dev/null +++ b/examples/nix_cross_compiling/README.md @@ -0,0 +1,3 @@ +# Nix + Bazel Crosspiling + +Blog Post: diff --git a/examples/nix_cross_compiling/WORKSPACE.bazel b/examples/nix_cross_compiling/WORKSPACE.bazel new file mode 100644 index 0000000000..f44f8229a4 --- /dev/null +++ b/examples/nix_cross_compiling/WORKSPACE.bazel @@ -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() diff --git a/examples/nix_cross_compiling/bazel/BUILD b/examples/nix_cross_compiling/bazel/BUILD new file mode 100644 index 0000000000..e69de29bb2 diff --git a/examples/nix_cross_compiling/bazel/nix_repositories.bzl b/examples/nix_cross_compiling/bazel/nix_repositories.bzl new file mode 100644 index 0000000000..69bf901ec5 --- /dev/null +++ b/examples/nix_cross_compiling/bazel/nix_repositories.bzl @@ -0,0 +1,217 @@ +load("@io_tweag_rules_nixpkgs//nixpkgs:nixpkgs.bzl", "nixpkgs_flake_package") + +_CONFIG_BUILD_FILE_CONTENT = """ +package(default_visibility = ["//visibility:public"]) + +exports_files(["config.bzl"]) +""" + +_RUST_BUILD_FILE_CONTENT = """ +load("@rules_rust//rust:toolchain.bzl", "rust_stdlib_filegroup") + +package(default_visibility = ["//visibility:public"]) + +# https://github.com/tweag/rules_nixpkgs/blob/master/toolchains/rust/rust.bzl#L33-L116 +filegroup( + name = "rustc", + srcs = ["bin/rustc"], +) + +filegroup( + name = "rustdoc", + srcs = ["bin/rustdoc"], +) + +filegroup( + name = "rustfmt", + srcs = ["bin/rustfmt"], +) + +filegroup( + name = "cargo", + srcs = ["bin/cargo"], +) + +filegroup( + name = "clippy_driver", + srcs = ["bin/clippy-driver"], +) + +filegroup( + name = "proc_macro_srv", + srcs = ["libexec/rust-analyzer-proc-macro-srv"], +) + +filegroup( + name = "rustc_lib", + srcs = glob( + [ + "bin/*.so", + "lib/*.so", + "lib/rustlib/x86_64-unknown-linux-gnu/codegen-backends/*.so", + "lib/rustlib/x86_64-unknown-linux-gnu/codegen-backends/*.dylib", + "lib/rustlib/x86_64-unknown-linux-gnu/bin/*", + "lib/rustlib/x86_64-unknown-linux-gnu/lib/*.so", + "lib/rustlib/x86_64-unknown-linux-gnu/lib/*.dylib", + ], + allow_empty = True, + ), +) + +filegroup( + name = "rust-src", + srcs = glob(["lib/rustlib/src/**/*"]), +) + +rust_stdlib_filegroup( + name = "rust_std-aarch64-apple-darwin", + srcs = glob( + [ + "rust/lib/rustlib/aarch64-apple-darwin/lib/*.rlib", + "rust/lib/rustlib/aarch64-apple-darwin/lib/*.so", + "rust/lib/rustlib/aarch64-apple-darwin/lib/*.dylib", + "rust/lib/rustlib/aarch64-apple-darwin/lib/*.a", + "rust/lib/rustlib/aarch64-apple-darwin/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, + ), +) + +rust_stdlib_filegroup( + name = "rust_std-aarch64-apple-ios", + srcs = glob( + [ + "rust/lib/rustlib/aarch64-apple-ios/lib/*.rlib", + "rust/lib/rustlib/aarch64-apple-ios/lib/*.so", + "rust/lib/rustlib/aarch64-apple-ios/lib/*.dylib", + "rust/lib/rustlib/aarch64-apple-ios/lib/*.a", + "rust/lib/rustlib/aarch64-apple-ios/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, + ), +) + +rust_stdlib_filegroup( + name = "rust_std-aarch64-linux-android", + srcs = glob( + [ + "rust/lib/rustlib/aarch64-linux-android/lib/*.rlib", + "rust/lib/rustlib/aarch64-linux-android/lib/*.so", + "rust/lib/rustlib/aarch64-linux-android/lib/*.dylib", + "rust/lib/rustlib/aarch64-linux-android/lib/*.a", + "rust/lib/rustlib/aarch64-linux-android/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, + ), +) + +rust_stdlib_filegroup( + name = "rust_std-aarch64-unknown-linux-gnu", + srcs = glob( + [ + "rust/lib/rustlib/aarch64-unknown-linux-gnu/lib/*.rlib", + "rust/lib/rustlib/aarch64-unknown-linux-gnu/lib/*.so", + "rust/lib/rustlib/aarch64-unknown-linux-gnu/lib/*.dylib", + "rust/lib/rustlib/aarch64-unknown-linux-gnu/lib/*.a", + "rust/lib/rustlib/aarch64-unknown-linux-gnu/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, + ), +) + +rust_stdlib_filegroup( + name = "rust_std-wasm32-unknown-unknown", + srcs = glob( + [ + "rust/lib/rustlib/wasm32-unknown-unknown/lib/*.rlib", + "rust/lib/rustlib/wasm32-unknown-unknown/lib/*.so", + "rust/lib/rustlib/wasm32-unknown-unknown/lib/*.dylib", + "rust/lib/rustlib/wasm32-unknown-unknown/lib/*.a", + "rust/lib/rustlib/wasm32-unknown-unknown/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, + ), +) + +rust_stdlib_filegroup( + name = "rust_std-wasm32-wasi", + srcs = glob( + [ + "rust/lib/rustlib/wasm32-wasi/lib/*.rlib", + "rust/lib/rustlib/wasm32-wasi/lib/*.so", + "rust/lib/rustlib/wasm32-wasi/lib/*.dylib", + "rust/lib/rustlib/wasm32-wasi/lib/*.a", + "rust/lib/rustlib/wasm32-wasi/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, + ), +) + +rust_stdlib_filegroup( + name = "rust_std-x86_64-apple-darwin", + srcs = glob( + [ + "rust/lib/rustlib/x86_64-apple-darwin/lib/*.rlib", + "rust/lib/rustlib/x86_64-apple-darwin/lib/*.so", + "rust/lib/rustlib/x86_64-apple-darwin/lib/*.dylib", + "rust/lib/rustlib/x86_64-apple-darwin/lib/*.a", + "rust/lib/rustlib/x86_64-apple-darwin/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, + ), +) + +rust_stdlib_filegroup( + name = "rust_std-x86_64-pc-windows-msvc", + srcs = glob( + [ + "rust/lib/rustlib/x86_64-pc-windows-msvc/lib/*.rlib", + "rust/lib/rustlib/x86_64-pc-windows-msvc/lib/*.so", + "rust/lib/rustlib/x86_64-pc-windows-msvc/lib/*.dylib", + "rust/lib/rustlib/x86_64-pc-windows-msvc/lib/*.a", + "rust/lib/rustlib/x86_64-pc-windows-msvc/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, + ), +) + +rust_stdlib_filegroup( + name = "rust_std-x86_64-unknown-linux-gnu", + srcs = glob( + [ + "rust/lib/rustlib/x86_64-unknown-linux-gnu/lib/*.rlib", + "rust/lib/rustlib/x86_64-unknown-linux-gnu/lib/*.so", + "rust/lib/rustlib/x86_64-unknown-linux-gnu/lib/*.dylib", + "rust/lib/rustlib/x86_64-unknown-linux-gnu/lib/*.a", + "rust/lib/rustlib/x86_64-unknown-linux-gnu/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, + ), +) +""" + +def nix_repositories(): + nixpkgs_flake_package( + name = "nix_config", + nix_flake_file = "//:flake.nix", + nix_flake_lock_file = "//:flake.lock", + package = "bazel.config", + build_file_content = _CONFIG_BUILD_FILE_CONTENT, + ) + + nixpkgs_flake_package( + name = "nix_rust", + nix_flake_file = "//:flake.nix", + nix_flake_lock_file = "//:flake.lock", + package = "bazel.rust", + build_file_content = _RUST_BUILD_FILE_CONTENT, + ) diff --git a/examples/nix_cross_compiling/bazel/platforms/BUILD b/examples/nix_cross_compiling/bazel/platforms/BUILD new file mode 100644 index 0000000000..55f7faca12 --- /dev/null +++ b/examples/nix_cross_compiling/bazel/platforms/BUILD @@ -0,0 +1,104 @@ +package(default_visibility = ["//visibility:public"]) + +# Host +platform( + name = "host", + constraint_values = ["@platforms//os:nixos"], + parents = ["@local_config_platform//:host"], +) + +# Platforms +platform( + name = "aarch64-apple-darwin", + constraint_values = [ + "@platforms//cpu:aarch64", + "@platforms//os:macos", + ], +) + +platform( + name = "aarch64-apple-ios", + constraint_values = [ + "@platforms//cpu:aarch64", + "@platforms//os:ios", + ], +) + +platform( + name = "aarch64-linux-android", + constraint_values = [ + "@platforms//cpu:aarch64", + "@platforms//os:android", + ], +) + +platform( + name = "aarch64-unknown-linux-gnu", + constraint_values = [ + "@platforms//cpu:aarch64", + "@platforms//os:linux", + ], +) + +platform( + name = "wasm32-unknown-unknown", + constraint_values = [ + "@platforms//cpu:wasm32", + "@platforms//os:none", + ], +) + +platform( + name = "wasm32-wasi", + constraint_values = [ + "@platforms//cpu:wasm32", + "@platforms//os:wasi", + ], +) + +platform( + name = "x86_64-apple-darwin", + constraint_values = [ + "@platforms//cpu:x86_64", + "@platforms//os:macos", + ], +) + +platform( + name = "x86_64-pc-windows-msvc", + constraint_values = [ + "@platforms//cpu:x86_64", + "@platforms//os:windows", + ], +) + +platform( + name = "x86_64-unknown-linux-gnu", + constraint_values = [ + "@platforms//cpu:x86_64", + "@platforms//os:linux", + ], +) + +platform( + name = "x86_64-unknown-nixos-gnu", + constraint_values = [ + "@platforms//cpu:x86_64", + "@platforms//os:nixos", + ], +) + +# Sentinel to catch implicit target platform usage +platform( + name = "missing", + constraint_values = [ + ":missing_constraint_value", + ], +) + +constraint_setting(name = "missing_constraint_setting") + +constraint_value( + name = "missing_constraint_value", + constraint_setting = ":missing_constraint_setting", +) diff --git a/examples/nix_cross_compiling/bazel/toolchain_rules/BUILD b/examples/nix_cross_compiling/bazel/toolchain_rules/BUILD new file mode 100644 index 0000000000..e69de29bb2 diff --git a/examples/nix_cross_compiling/bazel/toolchain_rules/cc_tools/BUILD b/examples/nix_cross_compiling/bazel/toolchain_rules/cc_tools/BUILD new file mode 100644 index 0000000000..e69de29bb2 diff --git a/examples/nix_cross_compiling/bazel/toolchain_rules/cc_tools/clang.bzl b/examples/nix_cross_compiling/bazel/toolchain_rules/cc_tools/clang.bzl new file mode 100644 index 0000000000..71537de5c7 --- /dev/null +++ b/examples/nix_cross_compiling/bazel/toolchain_rules/cc_tools/clang.bzl @@ -0,0 +1,164 @@ +load( + "@bazel_tools//tools/cpp:cc_toolchain_config_lib.bzl", + "action_config", + "flag_group", + "flag_set", + "tool", + "with_feature_set", +) +load("@bazel_tools//tools/build_defs/cc:action_names.bzl", "ACTION_NAMES") + +BAZEL_COMPILE_FLAG_SET = flag_set( + flag_groups = [ + flag_group( + flags = ["-MD", "-MF", "%{dependency_file}"], + expand_if_available = "dependency_file", + ), + flag_group( + flags = ["-frandom-seed=%{output_file}"], + expand_if_available = "output_file", + ), + flag_group( + flags = ["-D%{preprocessor_defines}"], + iterate_over = "preprocessor_defines", + ), + flag_group( + flags = ["-include", "%{includes}"], + iterate_over = "includes", + expand_if_available = "includes", + ), + flag_group( + flags = ["-iquote", "%{quote_include_paths}"], + iterate_over = "quote_include_paths", + ), + flag_group( + flags = ["-I%{include_paths}"], + iterate_over = "include_paths", + ), + flag_group( + flags = ["-isystem", "%{system_include_paths}"], + iterate_over = "system_include_paths", + ), + flag_group( + flags = ["-isystem", "%{external_include_paths}"], + iterate_over = "external_include_paths", + expand_if_available = "external_include_paths", + ), + flag_group( + flags = ["%{user_compile_flags}"], + iterate_over = "user_compile_flags", + expand_if_available = "user_compile_flags", + ), + flag_group( + flags = ["-c", "%{source_file}"], + expand_if_available = "source_file", + ), + flag_group( + flags = ["-o", "%{output_file}"], + expand_if_available = "output_file", + ), + ], +) + +def compile_action_configs( + clang, + target, + builtin_include_directories, + builtin_framework_directories, + compile_flags, + dbg_compile_flags, + fastbuild_compile_flags, + opt_compile_flags, + remap_path_prefix): + builtin_include_directory_compile_flags = [] + for builtin_include_directory in builtin_include_directories: + builtin_include_directory_compile_flags += ["-isystem", builtin_include_directory] + + builtin_framework_directory_compile_flags = [] + for builtin_framework_directory in builtin_framework_directories: + builtin_framework_directory_compile_flags += ["-iframework", builtin_framework_directory] + + required_compile_flags = ([ + "--target={}".format(target), + "-nostdinc", + + # `unix_cc_toolchain_config` + "-Wno-builtin-macro-redefined", + "-D__DATE__=\"redacted\"", + "-D__TIMESTAMP__=\"redacted\"", + "-D__TIME__=\"redacted\"", + "-fdebug-prefix-map=${{pwd}}={}".format(remap_path_prefix), + ] + + builtin_include_directory_compile_flags + + builtin_framework_directory_compile_flags) + required_compile_flag_set = flag_set( + flag_groups = ([ + flag_group(flags = required_compile_flags), + ] if required_compile_flags else []), + ) + + compile_flag_set = flag_set( + flag_groups = ([ + flag_group(flags = compile_flags), + ] if compile_flags else []), + ) + + dbg_compile_flag_set = flag_set( + flag_groups = ([ + flag_group(flags = dbg_compile_flags), + ] if dbg_compile_flags else []), + with_features = [with_feature_set(features = ["dbg"])], + ) + + fastbuild_compile_flag_set = flag_set( + flag_groups = ([ + flag_group(flags = fastbuild_compile_flags), + ] if fastbuild_compile_flags else []), + with_features = [with_feature_set(features = ["fastbuild"])], + ) + + opt_compile_flag_set = flag_set( + flag_groups = ([ + flag_group(flags = opt_compile_flags), + ] if opt_compile_flags else []), + with_features = [with_feature_set(features = ["opt"])], + ) + + return [ + action_config( + action_name = ACTION_NAMES.c_compile, + tools = [tool(path = "{}/bin/clang".format(clang))], + flag_sets = [ + required_compile_flag_set, + compile_flag_set, + dbg_compile_flag_set, + fastbuild_compile_flag_set, + opt_compile_flag_set, + BAZEL_COMPILE_FLAG_SET, + ], + ), + action_config( + action_name = ACTION_NAMES.cpp_compile, + tools = [tool(path = "{}/bin/clang".format(clang))], + flag_sets = [ + required_compile_flag_set, + compile_flag_set, + dbg_compile_flag_set, + fastbuild_compile_flag_set, + opt_compile_flag_set, + BAZEL_COMPILE_FLAG_SET, + ], + ), + action_config( + action_name = ACTION_NAMES.cpp_header_parsing, + tools = [tool(path = "{}/bin/clang".format(clang))], + flag_sets = [ + required_compile_flag_set, + compile_flag_set, + dbg_compile_flag_set, + fastbuild_compile_flag_set, + opt_compile_flag_set, + BAZEL_COMPILE_FLAG_SET, + ], + ), + ] diff --git a/examples/nix_cross_compiling/bazel/toolchain_rules/cc_tools/ld.lld.bzl b/examples/nix_cross_compiling/bazel/toolchain_rules/cc_tools/ld.lld.bzl new file mode 100644 index 0000000000..7120bf059b --- /dev/null +++ b/examples/nix_cross_compiling/bazel/toolchain_rules/cc_tools/ld.lld.bzl @@ -0,0 +1,286 @@ +load( + "@bazel_tools//tools/cpp:cc_toolchain_config_lib.bzl", + "action_config", + "flag_group", + "flag_set", + "tool", + "variable_with_value", + "with_feature_set", +) +load("@bazel_tools//tools/build_defs/cc:action_names.bzl", "ACTION_NAMES") + +BAZEL_LINK_FLAG_SET = flag_set( + flag_groups = [ + flag_group( + flags = ["%{linkstamp_paths}"], + iterate_over = "linkstamp_paths", + expand_if_available = "linkstamp_paths", + ), + flag_group( + iterate_over = "runtime_library_search_directories", + flag_groups = [ + flag_group( + flags = [ + "-Xlinker", + "-rpath", + "-Xlinker", + "$ORIGIN/%{runtime_library_search_directories}", + ], + ), + ], + expand_if_available = + "runtime_library_search_directories", + ), + flag_group( + flags = ["-L%{library_search_directories}"], + iterate_over = "library_search_directories", + expand_if_available = "library_search_directories", + ), + flag_group( + iterate_over = "libraries_to_link", + flag_groups = [ + flag_group( + flags = ["--start-lib"], + expand_if_equal = variable_with_value( + name = "libraries_to_link.type", + value = "object_file_group", + ), + ), + flag_group( + flags = ["-whole-archive"], + expand_if_true = + "libraries_to_link.is_whole_archive", + ), + flag_group( + flags = ["%{libraries_to_link.object_files}"], + iterate_over = "libraries_to_link.object_files", + expand_if_equal = variable_with_value( + name = "libraries_to_link.type", + value = "object_file_group", + ), + ), + flag_group( + flags = ["%{libraries_to_link.name}"], + expand_if_equal = variable_with_value( + name = "libraries_to_link.type", + value = "object_file", + ), + ), + flag_group( + flags = ["%{libraries_to_link.name}"], + expand_if_equal = variable_with_value( + name = "libraries_to_link.type", + value = "interface_library", + ), + ), + flag_group( + flags = ["%{libraries_to_link.name}"], + expand_if_equal = variable_with_value( + name = "libraries_to_link.type", + value = "static_library", + ), + ), + flag_group( + flags = ["-l%{libraries_to_link.name}"], + expand_if_equal = variable_with_value( + name = "libraries_to_link.type", + value = "dynamic_library", + ), + ), + flag_group( + flags = ["-l:%{libraries_to_link.name}"], + expand_if_equal = variable_with_value( + name = "libraries_to_link.type", + value = "versioned_dynamic_library", + ), + ), + flag_group( + flags = ["-no-whole-archive"], + expand_if_true = "libraries_to_link.is_whole_archive", + ), + flag_group( + flags = ["--end-lib"], + expand_if_equal = variable_with_value( + name = "libraries_to_link.type", + value = "object_file_group", + ), + ), + ], + expand_if_available = "libraries_to_link", + ), + flag_group( + flags = ["@%{thinlto_param_file}"], + expand_if_true = "thinlto_param_file", + ), + flag_group( + flags = ["%{user_link_flags}"], + iterate_over = "user_link_flags", + expand_if_available = "user_link_flags", + ), + flag_group( + flags = ["-o", "%{output_execpath}"], + expand_if_available = "output_execpath", + ), + ], +) + +def link_action_configs( + llvm, + target, + builtin_library_directories, + builtin_libraries, + builtin_framework_directories, + builtin_frameworks, + builtin_executable_objects, + link_flags, + dbg_link_flags, + fastbuild_link_flags, + opt_link_flags): + builtin_library_directory_link_flags = [] + for builtin_library_directory in builtin_library_directories: + builtin_library_directory_link_flags += ["-L", builtin_library_directory] + + builtin_library_link_flags = [] + for builtin_library in builtin_libraries: + builtin_library_link_flags += ["-l{}".format(builtin_library)] + + if builtin_framework_directories: + fail("Frameworks not supported by `ld.lld`") + + if builtin_frameworks: + fail("Frameworks not supported by `ld.lld`") + + builtin_executable_objects_link_flags = [] + for builtin_executable_object in builtin_executable_objects: + builtin_executable_objects_link_flags += ["-l{}".format(builtin_executable_object)] + + required_link_flags = (["-nostdlib"] + + builtin_library_directory_link_flags + + builtin_library_link_flags) + required_link_flag_set = flag_set( + flag_groups = ([ + flag_group(flags = required_link_flags), + ] if required_link_flags else []), + ) + + required_executable_link_flags = (builtin_executable_objects_link_flags) + required_executable_link_flag_set = flag_set( + flag_groups = ([ + flag_group(flags = required_executable_link_flags), + ] if required_executable_link_flags else []), + ) + + link_flag_set = flag_set( + flag_groups = ([ + flag_group(flags = link_flags), + ] if link_flags else []), + ) + + dbg_link_flag_set = flag_set( + flag_groups = ([ + flag_group(flags = dbg_link_flags), + ] if dbg_link_flags else []), + with_features = [with_feature_set(features = ["dbg"])], + ) + + fastbuild_link_flag_set = flag_set( + flag_groups = ([ + flag_group(flags = fastbuild_link_flags), + ] if fastbuild_link_flags else []), + with_features = [with_feature_set(features = ["fastbuild"])], + ) + + opt_link_flag_set = flag_set( + flag_groups = ([ + flag_group(flags = opt_link_flags), + ] if opt_link_flags else []), + with_features = [with_feature_set(features = ["opt"])], + ) + + return [ + action_config( + action_name = ACTION_NAMES.cpp_link_dynamic_library, + tools = [tool(path = "{}/bin/ld.lld".format(llvm))], + flag_sets = [ + # Mandatory, no link flags come through on command line. + flag_set( + flag_groups = [ + flag_group( + flags = ["@%{linker_param_file}"], + expand_if_available = "linker_param_file", + ), + ], + ), + required_link_flag_set, + link_flag_set, + dbg_link_flag_set, + fastbuild_link_flag_set, + opt_link_flag_set, + BAZEL_LINK_FLAG_SET, + flag_set( + flag_groups = [ + flag_group( + flags = ["-shared"], + ), + ], + ), + ], + ), + action_config( + action_name = ACTION_NAMES.cpp_link_nodeps_dynamic_library, + tools = [tool(path = "{}/bin/ld.lld".format(llvm))], + flag_sets = [ + # Mandatory, no link flags come through on command line. + flag_set( + flag_groups = [ + flag_group( + flags = ["@%{linker_param_file}"], + expand_if_available = "linker_param_file", + ), + ], + ), + required_link_flag_set, + link_flag_set, + dbg_link_flag_set, + fastbuild_link_flag_set, + opt_link_flag_set, + BAZEL_LINK_FLAG_SET, + flag_set( + flag_groups = [ + flag_group( + flags = ["-shared"], + ), + ], + ), + ], + ), + action_config( + action_name = ACTION_NAMES.cpp_link_executable, + tools = [tool(path = "{}/bin/ld.lld".format(llvm))], + flag_sets = [ + # Mandatory, no link flags come through on command line. + flag_set( + flag_groups = [ + flag_group( + flags = ["@%{linker_param_file}"], + expand_if_available = "linker_param_file", + ), + ], + ), + required_link_flag_set, + required_executable_link_flag_set, + link_flag_set, + dbg_link_flag_set, + fastbuild_link_flag_set, + opt_link_flag_set, + BAZEL_LINK_FLAG_SET, + flag_set( + flag_groups = [ + flag_group( + flags = ["-pie"], + ), + ], + ), + ], + ), + ] diff --git a/examples/nix_cross_compiling/bazel/toolchain_rules/cc_tools/ld64.lld.bzl b/examples/nix_cross_compiling/bazel/toolchain_rules/cc_tools/ld64.lld.bzl new file mode 100644 index 0000000000..8865f7e6d7 --- /dev/null +++ b/examples/nix_cross_compiling/bazel/toolchain_rules/cc_tools/ld64.lld.bzl @@ -0,0 +1,282 @@ +load( + "@bazel_tools//tools/cpp:cc_toolchain_config_lib.bzl", + "action_config", + "flag_group", + "flag_set", + "tool", + "variable_with_value", + "with_feature_set", +) +load("@bazel_tools//tools/build_defs/cc:action_names.bzl", "ACTION_NAMES") + +BAZEL_LINK_FLAG_SET = flag_set( + flag_groups = [ + flag_group( + flags = ["%{linkstamp_paths}"], + iterate_over = "linkstamp_paths", + expand_if_available = "linkstamp_paths", + ), + flag_group( + iterate_over = "runtime_library_search_directories", + flag_groups = [ + flag_group( + flags = [ + "-Xlinker", + "-rpath", + "-Xlinker", + "$ORIGIN/%{runtime_library_search_directories}", + ], + ), + ], + expand_if_available = + "runtime_library_search_directories", + ), + flag_group( + flags = ["-L%{library_search_directories}"], + iterate_over = "library_search_directories", + expand_if_available = "library_search_directories", + ), + flag_group( + iterate_over = "libraries_to_link", + flag_groups = [ + flag_group( + flags = ["--start-lib"], + expand_if_equal = variable_with_value( + name = "libraries_to_link.type", + value = "object_file_group", + ), + ), + flag_group( + flags = ["-whole-archive"], + expand_if_true = + "libraries_to_link.is_whole_archive", + ), + flag_group( + flags = ["%{libraries_to_link.object_files}"], + iterate_over = "libraries_to_link.object_files", + expand_if_equal = variable_with_value( + name = "libraries_to_link.type", + value = "object_file_group", + ), + ), + flag_group( + flags = ["%{libraries_to_link.name}"], + expand_if_equal = variable_with_value( + name = "libraries_to_link.type", + value = "object_file", + ), + ), + flag_group( + flags = ["%{libraries_to_link.name}"], + expand_if_equal = variable_with_value( + name = "libraries_to_link.type", + value = "interface_library", + ), + ), + flag_group( + flags = ["%{libraries_to_link.name}"], + expand_if_equal = variable_with_value( + name = "libraries_to_link.type", + value = "static_library", + ), + ), + flag_group( + flags = ["-l%{libraries_to_link.name}"], + expand_if_equal = variable_with_value( + name = "libraries_to_link.type", + value = "dynamic_library", + ), + ), + flag_group( + flags = ["-l:%{libraries_to_link.name}"], + expand_if_equal = variable_with_value( + name = "libraries_to_link.type", + value = "versioned_dynamic_library", + ), + ), + flag_group( + flags = ["-no-whole-archive"], + expand_if_true = "libraries_to_link.is_whole_archive", + ), + flag_group( + flags = ["--end-lib"], + expand_if_equal = variable_with_value( + name = "libraries_to_link.type", + value = "object_file_group", + ), + ), + ], + expand_if_available = "libraries_to_link", + ), + flag_group( + flags = ["@%{thinlto_param_file}"], + expand_if_true = "thinlto_param_file", + ), + flag_group( + flags = ["%{user_link_flags}"], + iterate_over = "user_link_flags", + expand_if_available = "user_link_flags", + ), + flag_group( + flags = ["-o", "%{output_execpath}"], + expand_if_available = "output_execpath", + ), + ], +) + +def link_action_configs( + llvm, + target, + builtin_library_directories, + builtin_libraries, + builtin_framework_directories, + builtin_frameworks, + builtin_executable_objects, + link_flags, + dbg_link_flags, + fastbuild_link_flags, + opt_link_flags): + builtin_library_directory_link_flags = [] + for builtin_library_directory in builtin_library_directories: + builtin_library_directory_link_flags += ["-L", builtin_library_directory] + + builtin_library_link_flags = [] + for builtin_library in builtin_libraries: + builtin_library_link_flags += ["-l{}".format(builtin_library)] + + builtin_framework_directory_link_flags = [] + for builtin_framework_directory in builtin_framework_directories: + builtin_framework_directory_link_flags += ["-F", builtin_framework_directory] + + builtin_framework_link_flags = [] + for builtin_framework in builtin_frameworks: + builtin_framework_link_flags += ["-framework", builtin_framework] + + builtin_executable_objects_link_flags = [] + for builtin_executable_object in builtin_executable_objects: + builtin_executable_objects_link_flags += ["-l{}".format(builtin_executable_object)] + + required_link_flags = (builtin_library_directory_link_flags + + builtin_library_link_flags + + builtin_framework_directory_link_flags + + builtin_framework_link_flags) + required_link_flag_set = flag_set( + flag_groups = ([ + flag_group(flags = required_link_flags), + ] if required_link_flags else []), + ) + + required_executable_link_flags = (builtin_executable_objects_link_flags) + required_executable_link_flag_set = flag_set( + flag_groups = ([ + flag_group(flags = required_executable_link_flags), + ] if required_executable_link_flags else []), + ) + + link_flag_set = flag_set( + flag_groups = ([ + flag_group(flags = link_flags), + ] if link_flags else []), + ) + + dbg_link_flag_set = flag_set( + flag_groups = ([ + flag_group(flags = dbg_link_flags), + ] if dbg_link_flags else []), + with_features = [with_feature_set(features = ["dbg"])], + ) + + fastbuild_link_flag_set = flag_set( + flag_groups = ([ + flag_group(flags = fastbuild_link_flags), + ] if fastbuild_link_flags else []), + with_features = [with_feature_set(features = ["fastbuild"])], + ) + + opt_link_flag_set = flag_set( + flag_groups = ([ + flag_group(flags = opt_link_flags), + ] if opt_link_flags else []), + with_features = [with_feature_set(features = ["opt"])], + ) + + return [ + action_config( + action_name = ACTION_NAMES.cpp_link_dynamic_library, + tools = [tool(path = "{}/bin/ld64.lld".format(llvm))], + flag_sets = [ + # Mandatory, no link flags come through on command line. + flag_set( + flag_groups = [ + flag_group( + flags = ["@%{linker_param_file}"], + expand_if_available = "linker_param_file", + ), + ], + ), + required_link_flag_set, + link_flag_set, + dbg_link_flag_set, + fastbuild_link_flag_set, + opt_link_flag_set, + BAZEL_LINK_FLAG_SET, + flag_set( + flag_groups = [ + flag_group( + flags = ["-dylib"], + ), + ], + ), + ], + ), + action_config( + action_name = ACTION_NAMES.cpp_link_nodeps_dynamic_library, + tools = [tool(path = "{}/bin/ld64.lld".format(llvm))], + flag_sets = [ + # Mandatory, no link flags come through on command line. + flag_set( + flag_groups = [ + flag_group( + flags = ["@%{linker_param_file}"], + expand_if_available = "linker_param_file", + ), + ], + ), + required_link_flag_set, + link_flag_set, + dbg_link_flag_set, + fastbuild_link_flag_set, + opt_link_flag_set, + BAZEL_LINK_FLAG_SET, + flag_set( + flag_groups = [ + flag_group( + flags = ["-dylib"], + ), + ], + ), + ], + ), + action_config( + action_name = ACTION_NAMES.cpp_link_executable, + tools = [tool(path = "{}/bin/ld64.lld".format(llvm))], + flag_sets = [ + # Mandatory, no link flags come through on command line. + flag_set( + flag_groups = [ + flag_group( + flags = ["@%{linker_param_file}"], + expand_if_available = "linker_param_file", + ), + ], + ), + required_link_flag_set, + required_executable_link_flag_set, + link_flag_set, + dbg_link_flag_set, + fastbuild_link_flag_set, + opt_link_flag_set, + BAZEL_LINK_FLAG_SET, + ], + ), + ] diff --git a/examples/nix_cross_compiling/bazel/toolchain_rules/cc_tools/lld-link.bzl b/examples/nix_cross_compiling/bazel/toolchain_rules/cc_tools/lld-link.bzl new file mode 100644 index 0000000000..81b5c55e46 --- /dev/null +++ b/examples/nix_cross_compiling/bazel/toolchain_rules/cc_tools/lld-link.bzl @@ -0,0 +1,279 @@ +load( + "@bazel_tools//tools/cpp:cc_toolchain_config_lib.bzl", + "action_config", + "flag_group", + "flag_set", + "tool", + "variable_with_value", + "with_feature_set", +) +load("@bazel_tools//tools/build_defs/cc:action_names.bzl", "ACTION_NAMES") + +BAZEL_LINK_FLAG_SET = flag_set( + flag_groups = [ + flag_group( + flags = ["%{linkstamp_paths}"], + iterate_over = "linkstamp_paths", + expand_if_available = "linkstamp_paths", + ), + flag_group( + iterate_over = "runtime_library_search_directories", + flag_groups = [ + flag_group( + flags = [ + "-Xlinker", + "-rpath", + "-Xlinker", + "$ORIGIN/%{runtime_library_search_directories}", + ], + ), + ], + expand_if_available = + "runtime_library_search_directories", + ), + flag_group( + flags = ["/libpath:%{library_search_directories}"], + iterate_over = "library_search_directories", + expand_if_available = "library_search_directories", + ), + flag_group( + iterate_over = "libraries_to_link", + flag_groups = [ + flag_group( + flags = ["/start-lib"], + expand_if_equal = variable_with_value( + name = "libraries_to_link.type", + value = "object_file_group", + ), + ), + flag_group( + flags = ["%{libraries_to_link.object_files}"], + iterate_over = "libraries_to_link.object_files", + expand_if_equal = variable_with_value( + name = "libraries_to_link.type", + value = "object_file_group", + ), + ), + flag_group( + flags = ["%{libraries_to_link.name}"], + expand_if_equal = variable_with_value( + name = "libraries_to_link.type", + value = "object_file", + ), + ), + flag_group( + flags = ["%{libraries_to_link.name}"], + expand_if_equal = variable_with_value( + name = "libraries_to_link.type", + value = "interface_library", + ), + ), + flag_group( + flag_groups = [ + flag_group( + flags = ["%{libraries_to_link.name}"], + expand_if_false = "libraries_to_link.is_whole_archive", + ), + flag_group( + flags = ["/wholearchive:%{libraries_to_link.name}"], + expand_if_true = "libraries_to_link.is_whole_archive", + ), + ], + expand_if_equal = variable_with_value( + name = "libraries_to_link.type", + value = "static_library", + ), + ), + flag_group( + flags = ["%{libraries_to_link.name}"], + expand_if_equal = variable_with_value( + name = "libraries_to_link.type", + value = "dynamic_library", + ), + ), + flag_group( + flags = ["%{libraries_to_link.name}"], + expand_if_equal = variable_with_value( + name = "libraries_to_link.type", + value = "versioned_dynamic_library", + ), + ), + flag_group( + flags = ["/end-lib"], + expand_if_equal = variable_with_value( + name = "libraries_to_link.type", + value = "object_file_group", + ), + ), + ], + expand_if_available = "libraries_to_link", + ), + flag_group( + flags = ["@%{thinlto_param_file}"], + expand_if_true = "thinlto_param_file", + ), + flag_group( + flags = ["%{user_link_flags}"], + iterate_over = "user_link_flags", + expand_if_available = "user_link_flags", + ), + flag_group( + flags = ["/out:%{output_execpath}"], + expand_if_available = "output_execpath", + ), + ], +) + +def link_action_configs( + llvm, + target, + builtin_executable_objects, + builtin_library_directories, + builtin_libraries, + builtin_framework_directories, + builtin_frameworks, + link_flags, + dbg_link_flags, + fastbuild_link_flags, + opt_link_flags): + builtin_library_directory_link_flags = [] + for builtin_library_directory in builtin_library_directories: + builtin_library_directory_link_flags += ["/libpath:{}".format(builtin_library_directory)] + + builtin_library_link_flags = [] + for builtin_library in builtin_libraries: + builtin_library_link_flags += [builtin_library] + + if builtin_framework_directories: + fail("Frameworks not supported by `lld-link`") + + if builtin_frameworks: + fail("Frameworks not supported by `lld-link`") + + builtin_executable_objects_link_flags = [] + for builtin_executable_object in builtin_executable_objects: + builtin_executable_objects_link_flags += [builtin_executable_object] + + required_link_flags = (["/nodefaultlib"] + + builtin_library_directory_link_flags + + builtin_library_link_flags) + required_link_flag_set = flag_set( + flag_groups = ([ + flag_group(flags = required_link_flags), + ] if required_link_flags else []), + ) + + required_executable_link_flags = (builtin_executable_objects_link_flags) + required_executable_link_flag_set = flag_set( + flag_groups = ([ + flag_group(flags = required_executable_link_flags), + ] if required_executable_link_flags else []), + ) + + link_flag_set = flag_set( + flag_groups = ([ + flag_group(flags = link_flags), + ] if link_flags else []), + ) + + dbg_link_flag_set = flag_set( + flag_groups = ([ + flag_group(flags = dbg_link_flags), + ] if dbg_link_flags else []), + with_features = [with_feature_set(features = ["dbg"])], + ) + + fastbuild_link_flag_set = flag_set( + flag_groups = ([ + flag_group(flags = fastbuild_link_flags), + ] if fastbuild_link_flags else []), + with_features = [with_feature_set(features = ["fastbuild"])], + ) + + opt_link_flag_set = flag_set( + flag_groups = ([ + flag_group(flags = opt_link_flags), + ] if opt_link_flags else []), + with_features = [with_feature_set(features = ["opt"])], + ) + + return [ + action_config( + action_name = ACTION_NAMES.cpp_link_dynamic_library, + tools = [tool(path = "{}/bin/lld-link".format(llvm))], + flag_sets = [ + # Mandatory, no link flags come through on command line. + flag_set( + flag_groups = [ + flag_group( + flags = ["@%{linker_param_file}"], + expand_if_available = "linker_param_file", + ), + ], + ), + required_link_flag_set, + link_flag_set, + dbg_link_flag_set, + fastbuild_link_flag_set, + opt_link_flag_set, + BAZEL_LINK_FLAG_SET, + flag_set( + flag_groups = [ + flag_group( + flags = ["/dll"], + ), + ], + ), + ], + ), + action_config( + action_name = ACTION_NAMES.cpp_link_nodeps_dynamic_library, + tools = [tool(path = "{}/bin/lld-link".format(llvm))], + flag_sets = [ + # Mandatory, no link flags come through on command line. + flag_set( + flag_groups = [ + flag_group( + flags = ["@%{linker_param_file}"], + expand_if_available = "linker_param_file", + ), + ], + ), + required_link_flag_set, + link_flag_set, + dbg_link_flag_set, + fastbuild_link_flag_set, + opt_link_flag_set, + BAZEL_LINK_FLAG_SET, + flag_set( + flag_groups = [ + flag_group( + flags = ["/dll"], + ), + ], + ), + ], + ), + action_config( + action_name = ACTION_NAMES.cpp_link_executable, + tools = [tool(path = "{}/bin/lld-link".format(llvm))], + flag_sets = [ + # Mandatory, no link flags come through on command line. + flag_set( + flag_groups = [ + flag_group( + flags = ["@%{linker_param_file}"], + expand_if_available = "linker_param_file", + ), + ], + ), + required_link_flag_set, + required_executable_link_flag_set, + link_flag_set, + dbg_link_flag_set, + fastbuild_link_flag_set, + opt_link_flag_set, + BAZEL_LINK_FLAG_SET, + ], + ), + ] diff --git a/examples/nix_cross_compiling/bazel/toolchain_rules/cc_tools/llvm-ar.bzl b/examples/nix_cross_compiling/bazel/toolchain_rules/cc_tools/llvm-ar.bzl new file mode 100644 index 0000000000..9f349ac38c --- /dev/null +++ b/examples/nix_cross_compiling/bazel/toolchain_rules/cc_tools/llvm-ar.bzl @@ -0,0 +1,63 @@ +load( + "@bazel_tools//tools/cpp:cc_toolchain_config_lib.bzl", + "action_config", + "flag_group", + "flag_set", + "tool", + "variable_with_value", +) +load("@bazel_tools//tools/build_defs/cc:action_names.bzl", "ACTION_NAMES") + +BASE_ARCHIVER_FLAG_SET = flag_set( + flag_groups = [ + flag_group( + flags = ["-rcsD", "%{output_execpath}"], + ), + flag_group( + iterate_over = "libraries_to_link", + flag_groups = [ + flag_group( + flags = ["%{libraries_to_link.name}"], + expand_if_equal = variable_with_value( + name = "libraries_to_link.type", + value = "object_file", + ), + ), + flag_group( + flags = ["%{libraries_to_link.object_files}"], + iterate_over = "libraries_to_link.object_files", + expand_if_equal = variable_with_value( + name = "libraries_to_link.type", + value = "object_file_group", + ), + ), + ], + expand_if_available = "libraries_to_link", + ), + ], +) + +def archive_action_configs(llvm, archive_flags): + archive_flag_set = flag_set( + flag_groups = ([ + flag_group(flags = archive_flags), + ] if archive_flags else []), + ) + + return [action_config( + action_name = ACTION_NAMES.cpp_link_static_library, + tools = [tool(path = "{}/bin/llvm-ar".format(llvm))], + flag_sets = [ + # Mandatory, no link flags come through on command line. + flag_set( + flag_groups = [ + flag_group( + flags = ["@%{linker_param_file}"], + expand_if_available = "linker_param_file", + ), + ], + ), + archive_flag_set, + BASE_ARCHIVER_FLAG_SET, + ], + )] diff --git a/examples/nix_cross_compiling/bazel/toolchain_rules/cc_tools/llvm-strip.bzl b/examples/nix_cross_compiling/bazel/toolchain_rules/cc_tools/llvm-strip.bzl new file mode 100644 index 0000000000..e5e219914d --- /dev/null +++ b/examples/nix_cross_compiling/bazel/toolchain_rules/cc_tools/llvm-strip.bzl @@ -0,0 +1,30 @@ +load( + "@bazel_tools//tools/cpp:cc_toolchain_config_lib.bzl", + "action_config", + "flag_group", + "flag_set", + "tool", +) +load("@bazel_tools//tools/build_defs/cc:action_names.bzl", "ACTION_NAMES") + +def strip_action_configs(llvm): + return [action_config( + action_name = ACTION_NAMES.strip, + tools = [tool(path = "{}/bin/llvm-strip".format(llvm))], + flag_sets = [ + flag_set( + flag_groups = [ + flag_group( + flags = ["-S", "-p", "-o", "%{output_file}"], + ), + flag_group( + iterate_over = "stripopts", + flags = ["%{stripopts}"], + ), + flag_group( + flags = ["%{input_file}"], + ), + ], + ), + ], + )] diff --git a/examples/nix_cross_compiling/bazel/toolchain_rules/cc_tools/wasm-ld.bzl b/examples/nix_cross_compiling/bazel/toolchain_rules/cc_tools/wasm-ld.bzl new file mode 100644 index 0000000000..b2eb031043 --- /dev/null +++ b/examples/nix_cross_compiling/bazel/toolchain_rules/cc_tools/wasm-ld.bzl @@ -0,0 +1,278 @@ +load( + "@bazel_tools//tools/cpp:cc_toolchain_config_lib.bzl", + "action_config", + "flag_group", + "flag_set", + "tool", + "variable_with_value", + "with_feature_set", +) +load("@bazel_tools//tools/build_defs/cc:action_names.bzl", "ACTION_NAMES") + +BAZEL_LINK_FLAG_SET = flag_set( + flag_groups = [ + flag_group( + flags = ["%{linkstamp_paths}"], + iterate_over = "linkstamp_paths", + expand_if_available = "linkstamp_paths", + ), + flag_group( + iterate_over = "runtime_library_search_directories", + flag_groups = [ + flag_group( + flags = [ + "-Xlinker", + "-rpath", + "-Xlinker", + "$ORIGIN/%{runtime_library_search_directories}", + ], + ), + ], + expand_if_available = + "runtime_library_search_directories", + ), + flag_group( + flags = ["-L%{library_search_directories}"], + iterate_over = "library_search_directories", + expand_if_available = "library_search_directories", + ), + flag_group( + iterate_over = "libraries_to_link", + flag_groups = [ + flag_group( + flags = ["--start-lib"], + expand_if_equal = variable_with_value( + name = "libraries_to_link.type", + value = "object_file_group", + ), + ), + flag_group( + flags = ["-whole-archive"], + expand_if_true = + "libraries_to_link.is_whole_archive", + ), + flag_group( + flags = ["%{libraries_to_link.object_files}"], + iterate_over = "libraries_to_link.object_files", + expand_if_equal = variable_with_value( + name = "libraries_to_link.type", + value = "object_file_group", + ), + ), + flag_group( + flags = ["%{libraries_to_link.name}"], + expand_if_equal = variable_with_value( + name = "libraries_to_link.type", + value = "object_file", + ), + ), + flag_group( + flags = ["%{libraries_to_link.name}"], + expand_if_equal = variable_with_value( + name = "libraries_to_link.type", + value = "interface_library", + ), + ), + flag_group( + flags = ["%{libraries_to_link.name}"], + expand_if_equal = variable_with_value( + name = "libraries_to_link.type", + value = "static_library", + ), + ), + flag_group( + flags = ["-l%{libraries_to_link.name}"], + expand_if_equal = variable_with_value( + name = "libraries_to_link.type", + value = "dynamic_library", + ), + ), + flag_group( + flags = ["-l:%{libraries_to_link.name}"], + expand_if_equal = variable_with_value( + name = "libraries_to_link.type", + value = "versioned_dynamic_library", + ), + ), + flag_group( + flags = ["-no-whole-archive"], + expand_if_true = "libraries_to_link.is_whole_archive", + ), + flag_group( + flags = ["--end-lib"], + expand_if_equal = variable_with_value( + name = "libraries_to_link.type", + value = "object_file_group", + ), + ), + ], + expand_if_available = "libraries_to_link", + ), + flag_group( + flags = ["@%{thinlto_param_file}"], + expand_if_true = "thinlto_param_file", + ), + flag_group( + flags = ["%{user_link_flags}"], + iterate_over = "user_link_flags", + expand_if_available = "user_link_flags", + ), + flag_group( + flags = ["-o", "%{output_execpath}"], + expand_if_available = "output_execpath", + ), + ], +) + +def link_action_configs( + llvm, + target, + builtin_library_directories, + builtin_libraries, + builtin_framework_directories, + builtin_frameworks, + builtin_executable_objects, + link_flags, + dbg_link_flags, + fastbuild_link_flags, + opt_link_flags): + builtin_library_directory_link_flags = [] + for builtin_library_directory in builtin_library_directories: + builtin_library_directory_link_flags += ["-L", builtin_library_directory] + + builtin_library_link_flags = [] + for builtin_library in builtin_libraries: + builtin_library_link_flags += ["-l{}".format(builtin_library)] + + if builtin_framework_directories: + fail("Frameworks not supported by `wasm-ld`") + + if builtin_frameworks: + fail("Frameworks not supported by `wasm-ld`") + + builtin_executable_objects_link_flags = [] + for builtin_executable_object in builtin_executable_objects: + builtin_executable_objects_link_flags += ["-l{}".format(builtin_executable_object)] + + required_link_flags = (builtin_library_directory_link_flags + + builtin_library_link_flags) + required_link_flag_set = flag_set( + flag_groups = ([ + flag_group(flags = required_link_flags), + ] if required_link_flags else []), + ) + + required_executable_link_flags = (builtin_executable_objects_link_flags) + required_executable_link_flag_set = flag_set( + flag_groups = ([ + flag_group(flags = required_executable_link_flags), + ] if required_executable_link_flags else []), + ) + + link_flag_set = flag_set( + flag_groups = ([ + flag_group(flags = link_flags), + ] if link_flags else []), + ) + + dbg_link_flag_set = flag_set( + flag_groups = ([ + flag_group(flags = dbg_link_flags), + ] if dbg_link_flags else []), + with_features = [with_feature_set(features = ["dbg"])], + ) + + fastbuild_link_flag_set = flag_set( + flag_groups = ([ + flag_group(flags = fastbuild_link_flags), + ] if fastbuild_link_flags else []), + with_features = [with_feature_set(features = ["fastbuild"])], + ) + + opt_link_flag_set = flag_set( + flag_groups = ([ + flag_group(flags = opt_link_flags), + ] if opt_link_flags else []), + with_features = [with_feature_set(features = ["opt"])], + ) + + return [ + action_config( + action_name = ACTION_NAMES.cpp_link_dynamic_library, + tools = [tool(path = "{}/bin/wasm-ld".format(llvm))], + flag_sets = [ + # Mandatory, no link flags come through on command line. + flag_set( + flag_groups = [ + flag_group( + flags = ["@%{linker_param_file}"], + expand_if_available = "linker_param_file", + ), + ], + ), + required_link_flag_set, + link_flag_set, + dbg_link_flag_set, + fastbuild_link_flag_set, + opt_link_flag_set, + BAZEL_LINK_FLAG_SET, + flag_set( + flag_groups = [ + flag_group( + flags = ["-shared"], + ), + ], + ), + ], + ), + action_config( + action_name = ACTION_NAMES.cpp_link_nodeps_dynamic_library, + tools = [tool(path = "{}/bin/wasm-ld".format(llvm))], + flag_sets = [ + # Mandatory, no link flags come through on command line. + flag_set( + flag_groups = [ + flag_group( + flags = ["@%{linker_param_file}"], + expand_if_available = "linker_param_file", + ), + ], + ), + required_link_flag_set, + link_flag_set, + dbg_link_flag_set, + fastbuild_link_flag_set, + opt_link_flag_set, + BAZEL_LINK_FLAG_SET, + flag_set( + flag_groups = [ + flag_group( + flags = ["-shared"], + ), + ], + ), + ], + ), + action_config( + action_name = ACTION_NAMES.cpp_link_executable, + tools = [tool(path = "{}/bin/wasm-ld".format(llvm))], + flag_sets = [ + # Mandatory, no link flags come through on command line. + flag_set( + flag_groups = [ + flag_group( + flags = ["@%{linker_param_file}"], + expand_if_available = "linker_param_file", + ), + ], + ), + required_link_flag_set, + required_executable_link_flag_set, + link_flag_set, + dbg_link_flag_set, + fastbuild_link_flag_set, + opt_link_flag_set, + BAZEL_LINK_FLAG_SET, + ], + ), + ] diff --git a/examples/nix_cross_compiling/bazel/toolchain_rules/llvm_cc_toolchain_config.bzl b/examples/nix_cross_compiling/bazel/toolchain_rules/llvm_cc_toolchain_config.bzl new file mode 100644 index 0000000000..75bdd0c87e --- /dev/null +++ b/examples/nix_cross_compiling/bazel/toolchain_rules/llvm_cc_toolchain_config.bzl @@ -0,0 +1,229 @@ +load( + "@bazel_tools//tools/cpp:cc_toolchain_config_lib.bzl", + "artifact_name_pattern", + "feature", +) +load("//bazel/toolchain_rules/cc_tools:clang.bzl", clang_compile_action_configs = "compile_action_configs") +load("//bazel/toolchain_rules/cc_tools:ld.lld.bzl", ld_lld_link_action_configs = "link_action_configs") +load("//bazel/toolchain_rules/cc_tools:ld64.lld.bzl", ld64_lld_link_action_configs = "link_action_configs") +load("//bazel/toolchain_rules/cc_tools:lld-link.bzl", lld_link_link_action_configs = "link_action_configs") +load("//bazel/toolchain_rules/cc_tools:llvm-ar.bzl", llvm_ar_archive_action_configs = "archive_action_configs") +load("//bazel/toolchain_rules/cc_tools:llvm-strip.bzl", llvm_strip_strip_action_configs = "strip_action_configs") +load("//bazel/toolchain_rules/cc_tools:wasm-ld.bzl", wasm_ld_link_action_configs = "link_action_configs") + +APPLE_ARTIFACT_NAME_PATTERNS = [ + # Artifact name patterns differ from the defaults only for dynamic libraries. + artifact_name_pattern( + category_name = "dynamic_library", + prefix = "lib", + extension = ".dylib", + ), +] + +LINUX_ARTIFACT_NAME_PATTERNS = [ + # Artifact name patterns are the default. +] + +WASM_ARTIFACT_NAME_PATTERNS = [ + # Artifact name patterns differ from the defaults only for executables. + artifact_name_pattern( + category_name = "executable", + prefix = "", + extension = ".wasm", + ), +] + +WINDOWS_ARTIFACT_NAME_PATTERNS = [ + artifact_name_pattern( + category_name = "object_file", + prefix = "", + extension = ".obj", + ), + artifact_name_pattern( + category_name = "static_library", + prefix = "", + extension = ".lib", + ), + artifact_name_pattern( + category_name = "alwayslink_static_library", + prefix = "", + extension = ".lo.lib", + ), + artifact_name_pattern( + category_name = "executable", + prefix = "", + extension = ".exe", + ), + artifact_name_pattern( + category_name = "dynamic_library", + prefix = "", + extension = ".dll", + ), + artifact_name_pattern( + category_name = "interface_library", + prefix = "", + extension = ".if.lib", + ), +] + +TARGET_CONFIG = { + "aarch64-apple-darwin": struct( + artifact_name_patterns = APPLE_ARTIFACT_NAME_PATTERNS, + compile_action_configs = clang_compile_action_configs, + archive_action_configs = llvm_ar_archive_action_configs, + link_action_configs = ld64_lld_link_action_configs, + strip_action_configs = llvm_strip_strip_action_configs, + ), + "aarch64-apple-ios": struct( + artifact_name_patterns = APPLE_ARTIFACT_NAME_PATTERNS, + compile_action_configs = clang_compile_action_configs, + archive_action_configs = llvm_ar_archive_action_configs, + link_action_configs = ld64_lld_link_action_configs, + strip_action_configs = llvm_strip_strip_action_configs, + ), + "aarch64-linux-android": struct( + artifact_name_patterns = LINUX_ARTIFACT_NAME_PATTERNS, + compile_action_configs = clang_compile_action_configs, + archive_action_configs = llvm_ar_archive_action_configs, + link_action_configs = ld_lld_link_action_configs, + strip_action_configs = llvm_strip_strip_action_configs, + ), + "aarch64-unknown-linux-gnu": struct( + artifact_name_patterns = LINUX_ARTIFACT_NAME_PATTERNS, + compile_action_configs = clang_compile_action_configs, + archive_action_configs = llvm_ar_archive_action_configs, + link_action_configs = ld_lld_link_action_configs, + strip_action_configs = llvm_strip_strip_action_configs, + ), + "wasm32-unknown-unknown": struct( + artifact_name_patterns = WASM_ARTIFACT_NAME_PATTERNS, + compile_action_configs = clang_compile_action_configs, + archive_action_configs = llvm_ar_archive_action_configs, + link_action_configs = wasm_ld_link_action_configs, + strip_action_configs = llvm_strip_strip_action_configs, + ), + "wasm32-wasi": struct( + artifact_name_patterns = WASM_ARTIFACT_NAME_PATTERNS, + compile_action_configs = clang_compile_action_configs, + archive_action_configs = llvm_ar_archive_action_configs, + link_action_configs = wasm_ld_link_action_configs, + strip_action_configs = llvm_strip_strip_action_configs, + ), + "x86_64-apple-darwin": struct( + artifact_name_patterns = APPLE_ARTIFACT_NAME_PATTERNS, + compile_action_configs = clang_compile_action_configs, + archive_action_configs = llvm_ar_archive_action_configs, + link_action_configs = ld64_lld_link_action_configs, + strip_action_configs = llvm_strip_strip_action_configs, + ), + "x86_64-pc-windows-msvc": struct( + artifact_name_patterns = WINDOWS_ARTIFACT_NAME_PATTERNS, + compile_action_configs = clang_compile_action_configs, + archive_action_configs = llvm_ar_archive_action_configs, + link_action_configs = lld_link_link_action_configs, + strip_action_configs = llvm_strip_strip_action_configs, + ), + "x86_64-unknown-linux-gnu": struct( + artifact_name_patterns = LINUX_ARTIFACT_NAME_PATTERNS, + compile_action_configs = clang_compile_action_configs, + archive_action_configs = llvm_ar_archive_action_configs, + link_action_configs = ld_lld_link_action_configs, + strip_action_configs = llvm_strip_strip_action_configs, + ), +} + +def _llvm_cc_toolchain_config_impl(ctx): + target_config = TARGET_CONFIG[ctx.attr.target] + + compile_action_configs = target_config.compile_action_configs( + clang = ctx.attr.clang, + target = ctx.attr.target, + builtin_include_directories = ctx.attr.builtin_include_directories, + builtin_framework_directories = ctx.attr.builtin_framework_directories, + compile_flags = ctx.attr.compile_flags, + dbg_compile_flags = ctx.attr.dbg_compile_flags, + fastbuild_compile_flags = ctx.attr.fastbuild_compile_flags, + opt_compile_flags = ctx.attr.opt_compile_flags, + remap_path_prefix = ctx.attr.remap_path_prefix, + ) + + archive_action_configs = target_config.archive_action_configs( + llvm = ctx.attr.llvm, + archive_flags = ctx.attr.archive_flags, + ) + + link_action_configs = target_config.link_action_configs( + llvm = ctx.attr.llvm, + target = ctx.attr.target, + builtin_library_directories = ctx.attr.builtin_library_directories, + builtin_libraries = ctx.attr.builtin_libraries, + builtin_framework_directories = ctx.attr.builtin_framework_directories, + builtin_frameworks = ctx.attr.builtin_frameworks, + builtin_executable_objects = ctx.attr.builtin_executable_objects, + link_flags = ctx.attr.link_flags, + dbg_link_flags = ctx.attr.dbg_link_flags, + fastbuild_link_flags = ctx.attr.fastbuild_link_flags, + opt_link_flags = ctx.attr.opt_link_flags, + ) + + strip_action_configs = target_config.strip_action_configs( + llvm = ctx.attr.llvm, + ) + + return cc_common.create_cc_toolchain_config_info( + ctx = ctx, + features = [ + feature(name = "no_legacy_features", enabled = True), + feature(name = "supports_start_end_lib", enabled = ctx.attr.supports_start_end_lib), + feature(name = "supports_interface_shared_libraries", enabled = False), + feature(name = "supports_dynamic_linker", enabled = True), + feature(name = "has_configured_linker_path", enabled = True), + feature(name = "static_link_cpp_runtimes", enabled = False), + feature(name = "supports_pic", enabled = True), + ], + action_configs = compile_action_configs + + archive_action_configs + + link_action_configs + + strip_action_configs, + artifact_name_patterns = target_config.artifact_name_patterns, + cxx_builtin_include_directories = ctx.attr.builtin_include_directories, + toolchain_identifier = ctx.attr.name, + host_system_name = None, + target_system_name = ctx.attr.target, + target_cpu = "unused", + target_libc = "unused", + compiler = "unused", + abi_version = None, + abi_libc_version = None, + tool_paths = [], + make_variables = [], + builtin_sysroot = None, + cc_target_os = None, + ) + +llvm_cc_toolchain_config = rule( + implementation = _llvm_cc_toolchain_config_impl, + attrs = { + "target": attr.string(mandatory = True), + "llvm": attr.string(mandatory = True), + "clang": attr.string(mandatory = True), + "builtin_include_directories": attr.string_list(), + "builtin_library_directories": attr.string_list(), + "builtin_libraries": attr.string_list(), + "builtin_framework_directories": attr.string_list(), + "builtin_frameworks": attr.string_list(), + "builtin_executable_objects": attr.string_list(), + "compile_flags": attr.string_list(), + "dbg_compile_flags": attr.string_list(), + "fastbuild_compile_flags": attr.string_list(), + "opt_compile_flags": attr.string_list(), + "link_flags": attr.string_list(), + "dbg_link_flags": attr.string_list(), + "fastbuild_link_flags": attr.string_list(), + "opt_link_flags": attr.string_list(), + "archive_flags": attr.string_list(), + "remap_path_prefix": attr.string(), + "supports_start_end_lib": attr.bool(default = True), + }, + provides = [CcToolchainConfigInfo], +) diff --git a/examples/nix_cross_compiling/bazel/toolchains/cc/aarch64-apple-darwin/BUILD b/examples/nix_cross_compiling/bazel/toolchains/cc/aarch64-apple-darwin/BUILD new file mode 100644 index 0000000000..a7e1859901 --- /dev/null +++ b/examples/nix_cross_compiling/bazel/toolchains/cc/aarch64-apple-darwin/BUILD @@ -0,0 +1,98 @@ +load( + "@nix_config//:config.bzl", + "CLANG", + "CLANG_LIB", + "CLANG_LIB_VERSION", + "LLVM", + "SDK_UNIVERSAL_APPLE_DARWIN", +) +load("//bazel/toolchain_rules:llvm_cc_toolchain_config.bzl", "llvm_cc_toolchain_config") + +llvm_cc_toolchain_config( + name = "cc_toolchain_config", + archive_flags = [], + builtin_executable_objects = [], + builtin_framework_directories = [ + "{}/System/Library/Frameworks".format(SDK_UNIVERSAL_APPLE_DARWIN), + ], + builtin_frameworks = [], + builtin_include_directories = [ + "{}/usr/include/c++/v1".format(SDK_UNIVERSAL_APPLE_DARWIN), + "{}/lib/clang/{}/include".format(CLANG_LIB, CLANG_LIB_VERSION), + "{}/usr/include".format(SDK_UNIVERSAL_APPLE_DARWIN), + ], + builtin_libraries = [], + builtin_library_directories = [ + "{}/usr/lib".format(SDK_UNIVERSAL_APPLE_DARWIN), + ], + clang = CLANG, + compile_flags = [ + "-fno-exceptions", + "-Werror", + "-Wall", + "-Wthread-safety", + "-Wself-assign", + ], + dbg_compile_flags = [ + "-g", + "-fstandalone-debug", + ], + dbg_link_flags = [], + fastbuild_compile_flags = [], + fastbuild_link_flags = [], + link_flags = [ + "-fatal_warnings", + "-arch", + "arm64", + "-macos_version_min", + "13.0.0", + "-platform_version", + "macos", + "13.0.0", + "13.1", + "-headerpad_max_install_names", + "-undefined", + "dynamic_lookup", + ], + llvm = LLVM, + opt_compile_flags = [ + "-g0", + "-O2", + "-D_FORTIFY_SOURCE=1", + "-DNDEBUG", + "-ffunction-sections", + "-fdata-sections", + ], + opt_link_flags = [ + "--gc-sections", + ], + target = "aarch64-apple-darwin", +) + +cc_toolchain( + name = "cc_toolchain", + all_files = "@nix_config//:config.bzl", + ar_files = "@nix_config//:config.bzl", + as_files = "@nix_config//:config.bzl", + compiler_files = "@nix_config//:config.bzl", + coverage_files = "@nix_config//:config.bzl", + dwp_files = "@nix_config//:config.bzl", + linker_files = "@nix_config//:config.bzl", + objcopy_files = "@nix_config//:config.bzl", + strip_files = "@nix_config//:config.bzl", + toolchain_config = "cc_toolchain_config", +) + +toolchain( + name = "toolchain", + exec_compatible_with = [ + "@platforms//cpu:x86_64", + "@platforms//os:nixos", + ], + target_compatible_with = [ + "@platforms//cpu:aarch64", + "@platforms//os:macos", + ], + toolchain = ":cc_toolchain", + toolchain_type = "@bazel_tools//tools/cpp:toolchain_type", +) diff --git a/examples/nix_cross_compiling/bazel/toolchains/cc/aarch64-apple-ios/BUILD b/examples/nix_cross_compiling/bazel/toolchains/cc/aarch64-apple-ios/BUILD new file mode 100644 index 0000000000..5b56e5a658 --- /dev/null +++ b/examples/nix_cross_compiling/bazel/toolchains/cc/aarch64-apple-ios/BUILD @@ -0,0 +1,96 @@ +load( + "@nix_config//:config.bzl", + "CLANG", + "CLANG_LIB", + "CLANG_LIB_VERSION", + "LLVM", + "SDK_UNIVERSAL_APPLE_IOS", +) +load("//bazel/toolchain_rules:llvm_cc_toolchain_config.bzl", "llvm_cc_toolchain_config") + +llvm_cc_toolchain_config( + name = "cc_toolchain_config", + archive_flags = [], + builtin_executable_objects = [], + builtin_framework_directories = [ + "{}/System/Library/Frameworks".format(SDK_UNIVERSAL_APPLE_IOS), + ], + builtin_frameworks = [], + builtin_include_directories = [ + "{}/usr/include/c++/v1".format(SDK_UNIVERSAL_APPLE_IOS), + "{}/lib/clang/{}/include".format(CLANG_LIB, CLANG_LIB_VERSION), + "{}/usr/include".format(SDK_UNIVERSAL_APPLE_IOS), + ], + builtin_libraries = [], + builtin_library_directories = [ + "{}/usr/lib".format(SDK_UNIVERSAL_APPLE_IOS), + ], + clang = CLANG, + compile_flags = [ + "-fno-exceptions", + "-Werror", + "-Wall", + "-Wthread-safety", + "-Wself-assign", + ], + dbg_compile_flags = [ + "-g", + "-fstandalone-debug", + ], + dbg_link_flags = [], + fastbuild_compile_flags = [], + fastbuild_link_flags = [], + link_flags = [ + "-fatal_warnings", + "-arch", + "arm64", + "-platform_version", + "ios", + "16.0.0", + "16.2", + "-headerpad_max_install_names", + "-undefined", + "dynamic_lookup", + ], + llvm = LLVM, + opt_compile_flags = [ + "-g0", + "-O2", + "-D_FORTIFY_SOURCE=1", + "-DNDEBUG", + "-ffunction-sections", + "-fdata-sections", + ], + opt_link_flags = [ + "--gc-sections", + ], + target = "aarch64-apple-ios", +) + +cc_toolchain( + name = "cc_toolchain", + all_files = "@nix_config//:config.bzl", + ar_files = "@nix_config//:config.bzl", + as_files = "@nix_config//:config.bzl", + compiler_files = "@nix_config//:config.bzl", + coverage_files = "@nix_config//:config.bzl", + dwp_files = "@nix_config//:config.bzl", + linker_files = "@nix_config//:config.bzl", + objcopy_files = "@nix_config//:config.bzl", + strip_files = "@nix_config//:config.bzl", + toolchain_config = "cc_toolchain_config", +) + +toolchain( + name = "toolchain", + exec_compatible_with = [ + "@platforms//cpu:x86_64", + "@platforms//os:nixos", + ], + target_compatible_with = [ + "@platforms//cpu:aarch64", + "@platforms//os:ios", + ], + toolchain = ":cc_toolchain", + toolchain_type = "@bazel_tools//tools/cpp:toolchain_type", +) diff --git a/examples/nix_cross_compiling/bazel/toolchains/cc/aarch64-linux-android/BUILD b/examples/nix_cross_compiling/bazel/toolchains/cc/aarch64-linux-android/BUILD new file mode 100644 index 0000000000..15ef95c600 --- /dev/null +++ b/examples/nix_cross_compiling/bazel/toolchains/cc/aarch64-linux-android/BUILD @@ -0,0 +1,111 @@ +load( + "@nix_config//:config.bzl", + "ANDROID_NDK_VERSION", + "CLANG", + "CLANG_LIB", + "CLANG_LIB_VERSION", + "LLVM", + "SDK_UNIVERSAL_LINUX_ANDROID", +) +load("//bazel/toolchain_rules:llvm_cc_toolchain_config.bzl", "llvm_cc_toolchain_config") + +llvm_cc_toolchain_config( + name = "cc_toolchain_config", + archive_flags = [], + builtin_executable_objects = [ + ":crtbegin_dynamic.o", + ":crtend_android.o", + ], + builtin_include_directories = [ + "{}/share/android-sdk/ndk/{}/toolchains/llvm/prebuilt/linux-x86_64/sysroot/usr/include/c++/v1".format(SDK_UNIVERSAL_LINUX_ANDROID, ANDROID_NDK_VERSION), + "{}/lib/clang/{}/include".format(CLANG_LIB, CLANG_LIB_VERSION), + "{}/share/android-sdk/ndk/{}/toolchains/llvm/prebuilt/linux-x86_64/sysroot/usr/local/include".format(SDK_UNIVERSAL_LINUX_ANDROID, ANDROID_NDK_VERSION), + "{}/share/android-sdk/ndk/{}/toolchains/llvm/prebuilt/linux-x86_64/sysroot/usr/include/aarch64-linux-android".format(SDK_UNIVERSAL_LINUX_ANDROID, ANDROID_NDK_VERSION), + "{}/share/android-sdk/ndk/{}/toolchains/llvm/prebuilt/linux-x86_64/sysroot/usr/include".format(SDK_UNIVERSAL_LINUX_ANDROID, ANDROID_NDK_VERSION), + ], + builtin_libraries = [ + "c", + "dl", + "m", + "unwind", + "stdc++", + "c++", + "c++abi", + ], + builtin_library_directories = [ + "{}/share/android-sdk/ndk/{}/toolchains/llvm/prebuilt/linux-x86_64/lib64/clang/14.0.7".format(SDK_UNIVERSAL_LINUX_ANDROID, ANDROID_NDK_VERSION), + "{}/share/android-sdk/ndk/{}/toolchains/llvm/prebuilt/linux-x86_64/lib64/clang/14.0.7/lib/linux/aarch64".format(SDK_UNIVERSAL_LINUX_ANDROID, ANDROID_NDK_VERSION), + "{}/share/android-sdk/ndk/{}/toolchains/llvm/prebuilt/linux-x86_64/sysroot/usr/lib/aarch64-linux-android/24".format(SDK_UNIVERSAL_LINUX_ANDROID, ANDROID_NDK_VERSION), + "{}/share/android-sdk/ndk/{}/toolchains/llvm/prebuilt/linux-x86_64/sysroot/usr/lib/aarch64-linux-android".format(SDK_UNIVERSAL_LINUX_ANDROID, ANDROID_NDK_VERSION), + "{}/share/android-sdk/ndk/{}/toolchains/llvm/prebuilt/linux-x86_64/sysroot/usr/lib".format(SDK_UNIVERSAL_LINUX_ANDROID, ANDROID_NDK_VERSION), + ], + clang = CLANG, + compile_flags = [ + "-DANDROID", + "-D_FORTIFY_SOURCE=2", + "-fdata-sections", + "-ffunction-sections", + "-fno-exceptions", + "-fstack-protector-strong", + "-funwind-tables", + "-no-canonical-prefixes", + "-Wall", + "-Werror", + "-Werror=format-security", + "-Wformat", + "-Wself-assign", + "-Wthread-safety", + ], + dbg_compile_flags = [ + "-g", + "-fno-limit-debug-info", + ], + dbg_link_flags = [], + fastbuild_compile_flags = [], + fastbuild_link_flags = [], + link_flags = [ + "--fatal-warnings", + "--sysroot={}/toolchains/llvm/prebuilt/linux-x86_64/sysroot".format(SDK_UNIVERSAL_LINUX_ANDROID), + "--dynamic-linker=/system/bin/linker64", + "--build-id=md5", + "--hash-style=gnu", + "-z", + "relro", + "-z", + "now", + ], + llvm = LLVM, + opt_compile_flags = [], + opt_link_flags = [ + "--gc-sections", + ], + target = "aarch64-linux-android", +) + +cc_toolchain( + name = "cc_toolchain", + all_files = "@nix_config//:config.bzl", + ar_files = "@nix_config//:config.bzl", + as_files = "@nix_config//:config.bzl", + compiler_files = "@nix_config//:config.bzl", + coverage_files = "@nix_config//:config.bzl", + dwp_files = "@nix_config//:config.bzl", + linker_files = "@nix_config//:config.bzl", + objcopy_files = "@nix_config//:config.bzl", + strip_files = "@nix_config//:config.bzl", + toolchain_config = "cc_toolchain_config", +) + +toolchain( + name = "toolchain", + exec_compatible_with = [ + "@platforms//cpu:x86_64", + "@platforms//os:nixos", + ], + target_compatible_with = [ + "@platforms//cpu:aarch64", + "@platforms//os:android", + ], + toolchain = ":cc_toolchain", + toolchain_type = "@bazel_tools//tools/cpp:toolchain_type", +) diff --git a/examples/nix_cross_compiling/bazel/toolchains/cc/aarch64-unknown-linux-gnu/BUILD b/examples/nix_cross_compiling/bazel/toolchains/cc/aarch64-unknown-linux-gnu/BUILD new file mode 100644 index 0000000000..73d285b971 --- /dev/null +++ b/examples/nix_cross_compiling/bazel/toolchains/cc/aarch64-unknown-linux-gnu/BUILD @@ -0,0 +1,100 @@ +load( + "@nix_config//:config.bzl", + "CLANG", + "CLANG_LIB", + "CLANG_LIB_VERSION", + "LLVM", + "SDK_AARCH64_UNKNOWN_LINUX_GNU", +) +load("//bazel/toolchain_rules:llvm_cc_toolchain_config.bzl", "llvm_cc_toolchain_config") + +llvm_cc_toolchain_config( + name = "cc_toolchain_config", + archive_flags = [], + builtin_executable_objects = [ + ":Scrt1.o", + ":crti.o", + ":crtbeginS.o", + ":crtendS.o", + ":crtn.o", + ], + builtin_include_directories = [ + "{}/usr/include/c++/10".format(SDK_AARCH64_UNKNOWN_LINUX_GNU), + "{}/usr/include/aarch64-linux-gnu/c++/10".format(SDK_AARCH64_UNKNOWN_LINUX_GNU), + "{}/usr/include/c++/10/backward".format(SDK_AARCH64_UNKNOWN_LINUX_GNU), + "{}/lib/clang/{}/include".format(CLANG_LIB, CLANG_LIB_VERSION), + "{}/usr/include/aarch64-linux-gnu".format(SDK_AARCH64_UNKNOWN_LINUX_GNU), + "{}/usr/include".format(SDK_AARCH64_UNKNOWN_LINUX_GNU), + ], + builtin_libraries = [ + "c", + "dl", + "gcc_s", + "gcc", + "m", + "pthread", + "stdc++", + ], + builtin_library_directories = [ + "{}/lib/aarch64-linux-gnu".format(SDK_AARCH64_UNKNOWN_LINUX_GNU), + "{}/usr/lib/gcc/aarch64-linux-gnu/10".format(SDK_AARCH64_UNKNOWN_LINUX_GNU), + "{}/usr/lib/aarch64-linux-gnu".format(SDK_AARCH64_UNKNOWN_LINUX_GNU), + ], + clang = CLANG, + compile_flags = [ + "-fno-exceptions", + "-Werror", + "-Wall", + "-Wthread-safety", + "-Wself-assign", + ], + dbg_compile_flags = [], + dbg_link_flags = [], + fastbuild_compile_flags = [], + fastbuild_link_flags = [], + link_flags = [ + "--fatal-warnings", + "--sysroot={}".format(SDK_AARCH64_UNKNOWN_LINUX_GNU), + "--dynamic-linker=/lib64/ld-linux-aarch64.so.1", + "--build-id=md5", + "--hash-style=gnu", + "-z", + "relro", + "-z", + "now", + ], + llvm = LLVM, + opt_compile_flags = [], + opt_link_flags = [ + "--gc-sections", + ], + target = "aarch64-unknown-linux-gnu", +) + +cc_toolchain( + name = "cc_toolchain", + all_files = "@nix_config//:config.bzl", + ar_files = "@nix_config//:config.bzl", + as_files = "@nix_config//:config.bzl", + compiler_files = "@nix_config//:config.bzl", + coverage_files = "@nix_config//:config.bzl", + dwp_files = "@nix_config//:config.bzl", + linker_files = "@nix_config//:config.bzl", + objcopy_files = "@nix_config//:config.bzl", + strip_files = "@nix_config//:config.bzl", + toolchain_config = "cc_toolchain_config", +) + +toolchain( + name = "toolchain", + exec_compatible_with = [ + "@platforms//cpu:x86_64", + "@platforms//os:nixos", + ], + target_compatible_with = [ + "@platforms//cpu:aarch64", + "@platforms//os:linux", + ], + toolchain = ":cc_toolchain", + toolchain_type = "@bazel_tools//tools/cpp:toolchain_type", +) diff --git a/examples/nix_cross_compiling/bazel/toolchains/cc/wasm32-unknown-unknown/BUILD b/examples/nix_cross_compiling/bazel/toolchains/cc/wasm32-unknown-unknown/BUILD new file mode 100644 index 0000000000..4b0a38f690 --- /dev/null +++ b/examples/nix_cross_compiling/bazel/toolchains/cc/wasm32-unknown-unknown/BUILD @@ -0,0 +1,72 @@ +load( + "@nix_config//:config.bzl", + "CLANG", + "CLANG_LIB", + "CLANG_LIB_VERSION", + "LIBCLANG_RT_WASM32", + "LLVM", +) +load("//bazel/toolchain_rules:llvm_cc_toolchain_config.bzl", "llvm_cc_toolchain_config") + +llvm_cc_toolchain_config( + name = "cc_toolchain_config", + archive_flags = [], + builtin_executable_objects = [], + builtin_include_directories = [ + "{}/lib/clang/{}/include".format(CLANG_LIB, CLANG_LIB_VERSION), + ], + builtin_libraries = [], + builtin_library_directories = [], + clang = CLANG, + compile_flags = [ + "-fno-exceptions", + "-Werror", + "-Wall", + "-Wthread-safety", + "-Wself-assign", + ], + dbg_compile_flags = [], + dbg_link_flags = [], + fastbuild_compile_flags = [], + fastbuild_link_flags = [], + link_flags = [ + "--fatal-warnings", + # `wasm-ld` doesn't respect the verbatim `-l:` syntax. + "{}/lib/wasi/libclang_rt.builtins-wasm32.a".format(LIBCLANG_RT_WASM32), + ], + llvm = LLVM, + opt_compile_flags = [], + opt_link_flags = [ + "--gc-sections", + ], + supports_start_end_lib = False, + target = "wasm32-unknown-unknown", +) + +cc_toolchain( + name = "cc_toolchain", + all_files = "@nix_config//:config.bzl", + ar_files = "@nix_config//:config.bzl", + as_files = "@nix_config//:config.bzl", + compiler_files = "@nix_config//:config.bzl", + coverage_files = "@nix_config//:config.bzl", + dwp_files = "@nix_config//:config.bzl", + linker_files = "@nix_config//:config.bzl", + objcopy_files = "@nix_config//:config.bzl", + strip_files = "@nix_config//:config.bzl", + toolchain_config = "cc_toolchain_config", +) + +toolchain( + name = "toolchain", + exec_compatible_with = [ + "@platforms//cpu:x86_64", + "@platforms//os:nixos", + ], + target_compatible_with = [ + "@platforms//cpu:wasm32", + "@platforms//os:none", + ], + toolchain = ":cc_toolchain", + toolchain_type = "@bazel_tools//tools/cpp:toolchain_type", +) diff --git a/examples/nix_cross_compiling/bazel/toolchains/cc/wasm32-wasi/BUILD b/examples/nix_cross_compiling/bazel/toolchains/cc/wasm32-wasi/BUILD new file mode 100644 index 0000000000..96b9120f69 --- /dev/null +++ b/examples/nix_cross_compiling/bazel/toolchains/cc/wasm32-wasi/BUILD @@ -0,0 +1,83 @@ +load( + "@nix_config//:config.bzl", + "CLANG", + "CLANG_LIB", + "CLANG_LIB_VERSION", + "LIBCLANG_RT_WASM32", + "LLVM", + "SDK_WASM32_WASI", +) +load("//bazel/toolchain_rules:llvm_cc_toolchain_config.bzl", "llvm_cc_toolchain_config") + +llvm_cc_toolchain_config( + name = "cc_toolchain_config", + archive_flags = [], + builtin_executable_objects = [], + builtin_include_directories = [ + "{}/wasi-sysroot/include/c++/v1".format(SDK_WASM32_WASI), + "{}/lib/clang/{}/include".format(CLANG_LIB, CLANG_LIB_VERSION), + "{}/wasi-sysroot/include".format(SDK_WASM32_WASI), + ], + builtin_libraries = [ + "c", + "c++", + "c++abi", + "m", + ], + builtin_library_directories = [ + "{}/wasi-sysroot/lib/wasm32-wasi".format(SDK_WASM32_WASI), + ], + clang = CLANG, + compile_flags = [ + "-fno-exceptions", + "-Werror", + "-Wall", + "-Wthread-safety", + "-Wself-assign", + ], + dbg_compile_flags = [], + dbg_link_flags = [], + fastbuild_compile_flags = [], + fastbuild_link_flags = [], + link_flags = [ + "--fatal-warnings", + # `wasm-ld` doesn't respect the verbatim `-l:` syntax. + "{}/lib/wasi/libclang_rt.builtins-wasm32.a".format(LIBCLANG_RT_WASM32), + "{}/wasi-sysroot/lib/wasm32-wasi/crt1.o".format(SDK_WASM32_WASI), + ], + llvm = LLVM, + opt_compile_flags = [], + opt_link_flags = [ + "--gc-sections", + ], + supports_start_end_lib = False, + target = "wasm32-wasi", +) + +cc_toolchain( + name = "cc_toolchain", + all_files = "@nix_config//:config.bzl", + ar_files = "@nix_config//:config.bzl", + as_files = "@nix_config//:config.bzl", + compiler_files = "@nix_config//:config.bzl", + coverage_files = "@nix_config//:config.bzl", + dwp_files = "@nix_config//:config.bzl", + linker_files = "@nix_config//:config.bzl", + objcopy_files = "@nix_config//:config.bzl", + strip_files = "@nix_config//:config.bzl", + toolchain_config = "cc_toolchain_config", +) + +toolchain( + name = "toolchain", + exec_compatible_with = [ + "@platforms//cpu:x86_64", + "@platforms//os:nixos", + ], + target_compatible_with = [ + "@platforms//cpu:wasm32", + "@platforms//os:wasi", + ], + toolchain = ":cc_toolchain", + toolchain_type = "@bazel_tools//tools/cpp:toolchain_type", +) diff --git a/examples/nix_cross_compiling/bazel/toolchains/cc/x86_64-apple-darwin/BUILD b/examples/nix_cross_compiling/bazel/toolchains/cc/x86_64-apple-darwin/BUILD new file mode 100644 index 0000000000..24c713e24c --- /dev/null +++ b/examples/nix_cross_compiling/bazel/toolchains/cc/x86_64-apple-darwin/BUILD @@ -0,0 +1,98 @@ +load( + "@nix_config//:config.bzl", + "CLANG", + "CLANG_LIB", + "CLANG_LIB_VERSION", + "LLVM", + "SDK_UNIVERSAL_APPLE_DARWIN", +) +load("//bazel/toolchain_rules:llvm_cc_toolchain_config.bzl", "llvm_cc_toolchain_config") + +llvm_cc_toolchain_config( + name = "cc_toolchain_config", + archive_flags = [], + builtin_executable_objects = [], + builtin_framework_directories = [ + "{}/System/Library/Frameworks".format(SDK_UNIVERSAL_APPLE_DARWIN), + ], + builtin_frameworks = [], + builtin_include_directories = [ + "{}/usr/include/c++/v1".format(SDK_UNIVERSAL_APPLE_DARWIN), + "{}/lib/clang/{}/include".format(CLANG_LIB, CLANG_LIB_VERSION), + "{}/usr/include".format(SDK_UNIVERSAL_APPLE_DARWIN), + ], + builtin_libraries = [], + builtin_library_directories = [ + "{}/usr/lib".format(SDK_UNIVERSAL_APPLE_DARWIN), + ], + clang = CLANG, + compile_flags = [ + "-fno-exceptions", + "-Werror", + "-Wall", + "-Wthread-safety", + "-Wself-assign", + ], + dbg_compile_flags = [ + "-g", + "-fstandalone-debug", + ], + dbg_link_flags = [], + fastbuild_compile_flags = [], + fastbuild_link_flags = [], + link_flags = [ + "-fatal_warnings", + "-arch", + "x86_64", + "-macos_version_min", + "13.0.0", + "-platform_version", + "macos", + "13.0.0", + "13.1", + "-headerpad_max_install_names", + "-undefined", + "dynamic_lookup", + ], + llvm = LLVM, + opt_compile_flags = [ + "-g0", + "-O2", + "-D_FORTIFY_SOURCE=1", + "-DNDEBUG", + "-ffunction-sections", + "-fdata-sections", + ], + opt_link_flags = [ + "--gc-sections", + ], + target = "x86_64-apple-darwin", +) + +cc_toolchain( + name = "cc_toolchain", + all_files = "@nix_config//:config.bzl", + ar_files = "@nix_config//:config.bzl", + as_files = "@nix_config//:config.bzl", + compiler_files = "@nix_config//:config.bzl", + coverage_files = "@nix_config//:config.bzl", + dwp_files = "@nix_config//:config.bzl", + linker_files = "@nix_config//:config.bzl", + objcopy_files = "@nix_config//:config.bzl", + strip_files = "@nix_config//:config.bzl", + toolchain_config = "cc_toolchain_config", +) + +toolchain( + name = "toolchain", + exec_compatible_with = [ + "@platforms//cpu:x86_64", + "@platforms//os:nixos", + ], + target_compatible_with = [ + "@platforms//cpu:x86_64", + "@platforms//os:macos", + ], + toolchain = ":cc_toolchain", + toolchain_type = "@bazel_tools//tools/cpp:toolchain_type", +) diff --git a/examples/nix_cross_compiling/bazel/toolchains/cc/x86_64-pc-windows-msvc/BUILD b/examples/nix_cross_compiling/bazel/toolchains/cc/x86_64-pc-windows-msvc/BUILD new file mode 100644 index 0000000000..e570ff0798 --- /dev/null +++ b/examples/nix_cross_compiling/bazel/toolchains/cc/x86_64-pc-windows-msvc/BUILD @@ -0,0 +1,90 @@ +load( + "@nix_config//:config.bzl", + "CLANG", + "CLANG_LIB", + "CLANG_LIB_VERSION", + "LLVM", + "SDK_X86_64_PC_WINDOWS_MSVC", +) +load("//bazel/toolchain_rules:llvm_cc_toolchain_config.bzl", "llvm_cc_toolchain_config") + +llvm_cc_toolchain_config( + name = "cc_toolchain_config", + archive_flags = [], + builtin_executable_objects = [], + builtin_include_directories = [ + "{}/lib/clang/{}/include".format(CLANG_LIB, CLANG_LIB_VERSION), + "{}/crt/include".format(SDK_X86_64_PC_WINDOWS_MSVC), + "{}/sdk/include/ucrt".format(SDK_X86_64_PC_WINDOWS_MSVC), + "{}/sdk/include/shared".format(SDK_X86_64_PC_WINDOWS_MSVC), + "{}/sdk/include/um".format(SDK_X86_64_PC_WINDOWS_MSVC), + "{}/sdk/include/winrt".format(SDK_X86_64_PC_WINDOWS_MSVC), + ], + builtin_libraries = [ + "advapi32.lib", + "bcrypt.lib", + "kernel32.lib", + "libcmt.lib", + "libcpmt.lib", + "libucrt.lib", + "libvcruntime.lib", + "ntdll.lib", + "userenv.lib", + "uuid.lib", + "ws2_32.lib", + ], + builtin_library_directories = [ + "{}/crt/lib/x86_64".format(SDK_X86_64_PC_WINDOWS_MSVC), + "{}/sdk/lib/um/x86_64".format(SDK_X86_64_PC_WINDOWS_MSVC), + "{}/sdk/lib/ucrt/x86_64".format(SDK_X86_64_PC_WINDOWS_MSVC), + ], + clang = CLANG, + compile_flags = [ + "-fno-exceptions", + "-Werror", + "-Wall", + "-Wthread-safety", + "-Wself-assign", + ], + dbg_compile_flags = [], + dbg_link_flags = [], + fastbuild_compile_flags = [], + fastbuild_link_flags = [], + link_flags = [ + "/wx", # Treat warnings as errors + ], + llvm = LLVM, + opt_compile_flags = [], + opt_link_flags = [ + "/opt:icf,ref", + ], + target = "x86_64-pc-windows-msvc", +) + +cc_toolchain( + name = "cc_toolchain", + all_files = "@nix_config//:config.bzl", + ar_files = "@nix_config//:config.bzl", + as_files = "@nix_config//:config.bzl", + compiler_files = "@nix_config//:config.bzl", + coverage_files = "@nix_config//:config.bzl", + dwp_files = "@nix_config//:config.bzl", + linker_files = "@nix_config//:config.bzl", + objcopy_files = "@nix_config//:config.bzl", + strip_files = "@nix_config//:config.bzl", + toolchain_config = "cc_toolchain_config", +) + +toolchain( + name = "toolchain", + exec_compatible_with = [ + "@platforms//cpu:x86_64", + "@platforms//os:nixos", + ], + target_compatible_with = [ + "@platforms//cpu:x86_64", + "@platforms//os:windows", + ], + toolchain = ":cc_toolchain", + toolchain_type = "@bazel_tools//tools/cpp:toolchain_type", +) diff --git a/examples/nix_cross_compiling/bazel/toolchains/cc/x86_64-unknown-linux-gnu/BUILD b/examples/nix_cross_compiling/bazel/toolchains/cc/x86_64-unknown-linux-gnu/BUILD new file mode 100644 index 0000000000..1a8bfd998f --- /dev/null +++ b/examples/nix_cross_compiling/bazel/toolchains/cc/x86_64-unknown-linux-gnu/BUILD @@ -0,0 +1,100 @@ +load( + "@nix_config//:config.bzl", + "CLANG", + "CLANG_LIB", + "CLANG_LIB_VERSION", + "LLVM", + "SDK_X86_64_UNKNOWN_LINUX_GNU", +) +load("//bazel/toolchain_rules:llvm_cc_toolchain_config.bzl", "llvm_cc_toolchain_config") + +llvm_cc_toolchain_config( + name = "cc_toolchain_config", + archive_flags = [], + builtin_executable_objects = [ + ":Scrt1.o", + ":crti.o", + ":crtbeginS.o", + ":crtendS.o", + ":crtn.o", + ], + builtin_include_directories = [ + "{}/usr/include/c++/10".format(SDK_X86_64_UNKNOWN_LINUX_GNU), + "{}/usr/include/x86_64-linux-gnu/c++/10".format(SDK_X86_64_UNKNOWN_LINUX_GNU), + "{}/usr/include/c++/10/backward".format(SDK_X86_64_UNKNOWN_LINUX_GNU), + "{}/lib/clang/{}/include".format(CLANG_LIB, CLANG_LIB_VERSION), + "{}/usr/include/x86_64-linux-gnu".format(SDK_X86_64_UNKNOWN_LINUX_GNU), + "{}/usr/include".format(SDK_X86_64_UNKNOWN_LINUX_GNU), + ], + builtin_libraries = [ + "c", + "dl", + "gcc_s", + "gcc", + "m", + "pthread", + "stdc++", + ], + builtin_library_directories = [ + "{}/lib/x86_64-linux-gnu".format(SDK_X86_64_UNKNOWN_LINUX_GNU), + "{}/usr/lib/gcc/x86_64-linux-gnu/10".format(SDK_X86_64_UNKNOWN_LINUX_GNU), + "{}/usr/lib/x86_64-linux-gnu".format(SDK_X86_64_UNKNOWN_LINUX_GNU), + ], + clang = CLANG, + compile_flags = [ + "-fno-exceptions", + "-Werror", + "-Wall", + "-Wthread-safety", + "-Wself-assign", + ], + dbg_compile_flags = [], + dbg_link_flags = [], + fastbuild_compile_flags = [], + fastbuild_link_flags = [], + link_flags = [ + "--fatal-warnings", + "--sysroot={}".format(SDK_X86_64_UNKNOWN_LINUX_GNU), + "--dynamic-linker=/lib64/ld-linux-x86-64.so.2", + "--build-id=md5", + "--hash-style=gnu", + "-z", + "relro", + "-z", + "now", + ], + llvm = LLVM, + opt_compile_flags = [], + opt_link_flags = [ + "--gc-sections", + ], + target = "x86_64-unknown-linux-gnu", +) + +cc_toolchain( + name = "cc_toolchain", + all_files = "@nix_config//:config.bzl", + ar_files = "@nix_config//:config.bzl", + as_files = "@nix_config//:config.bzl", + compiler_files = "@nix_config//:config.bzl", + coverage_files = "@nix_config//:config.bzl", + dwp_files = "@nix_config//:config.bzl", + linker_files = "@nix_config//:config.bzl", + objcopy_files = "@nix_config//:config.bzl", + strip_files = "@nix_config//:config.bzl", + toolchain_config = "cc_toolchain_config", +) + +toolchain( + name = "toolchain", + exec_compatible_with = [ + "@platforms//cpu:x86_64", + "@platforms//os:nixos", + ], + target_compatible_with = [ + "@platforms//cpu:x86_64", + "@platforms//os:linux", + ], + toolchain = ":cc_toolchain", + toolchain_type = "@bazel_tools//tools/cpp:toolchain_type", +) diff --git a/examples/nix_cross_compiling/bazel/toolchains/cc/x86_64-unknown-nixos-gnu/BUILD b/examples/nix_cross_compiling/bazel/toolchains/cc/x86_64-unknown-nixos-gnu/BUILD new file mode 100644 index 0000000000..5447fbc87a --- /dev/null +++ b/examples/nix_cross_compiling/bazel/toolchains/cc/x86_64-unknown-nixos-gnu/BUILD @@ -0,0 +1,96 @@ +load( + "@nix_config//:config.bzl", + "CLANG", + "CLANG_LIB", + "CLANG_LIB_VERSION", + "LLVM", + "NIXOS_DYNAMIC_LINKER", + "SDK_X86_64_UNKNOWN_NIXOS_GNU", +) +load("//bazel/toolchain_rules:llvm_cc_toolchain_config.bzl", "llvm_cc_toolchain_config") + +llvm_cc_toolchain_config( + name = "cc_toolchain_config", + archive_flags = [], + builtin_executable_objects = [ + ":Scrt1.o", + ":crti.o", + ":crtbeginS.o", + ":crtendS.o", + ":crtn.o", + ], + builtin_include_directories = [ + "{}/include/c++/v1".format(SDK_X86_64_UNKNOWN_NIXOS_GNU), + "{}/lib/clang/{}/include".format(CLANG_LIB, CLANG_LIB_VERSION), + "{}/include".format(SDK_X86_64_UNKNOWN_NIXOS_GNU), + ], + builtin_libraries = [ + ":libunwind.a", + ":libc.so", + ":libc++.a", + ":libc++abi.a", + "dl", + "m", + "pthread", + ], + builtin_library_directories = [ + "{}/lib".format(SDK_X86_64_UNKNOWN_NIXOS_GNU), + "{}/lib/gcc/x86_64-unknown-linux-gnu/13.1.0".format(SDK_X86_64_UNKNOWN_NIXOS_GNU), + ], + clang = CLANG, + compile_flags = [ + "-fno-exceptions", + "-Werror", + "-Wall", + "-Wthread-safety", + "-Wself-assign", + ], + dbg_compile_flags = [], + dbg_link_flags = [], + fastbuild_compile_flags = [], + fastbuild_link_flags = [], + link_flags = [ + "--fatal-warnings", + "--dynamic-linker={}".format(NIXOS_DYNAMIC_LINKER), + "--build-id=md5", + "--hash-style=gnu", + "-z", + "relro", + "-z", + "now", + ], + llvm = LLVM, + opt_compile_flags = [], + opt_link_flags = [ + "--gc-sections", + ], + target = "x86_64-unknown-linux-gnu", +) + +cc_toolchain( + name = "cc_toolchain", + all_files = "@nix_config//:config.bzl", + ar_files = "@nix_config//:config.bzl", + as_files = "@nix_config//:config.bzl", + compiler_files = "@nix_config//:config.bzl", + coverage_files = "@nix_config//:config.bzl", + dwp_files = "@nix_config//:config.bzl", + linker_files = "@nix_config//:config.bzl", + objcopy_files = "@nix_config//:config.bzl", + strip_files = "@nix_config//:config.bzl", + toolchain_config = "cc_toolchain_config", +) + +toolchain( + name = "toolchain", + exec_compatible_with = [ + "@platforms//cpu:x86_64", + "@platforms//os:nixos", + ], + target_compatible_with = [ + "@platforms//cpu:x86_64", + "@platforms//os:nixos", + ], + toolchain = ":cc_toolchain", + toolchain_type = "@bazel_tools//tools/cpp:toolchain_type", +) diff --git a/examples/nix_cross_compiling/bazel/toolchains/rust/aarch64-apple-darwin/BUILD b/examples/nix_cross_compiling/bazel/toolchains/rust/aarch64-apple-darwin/BUILD new file mode 100644 index 0000000000..ba32a6d16b --- /dev/null +++ b/examples/nix_cross_compiling/bazel/toolchains/rust/aarch64-apple-darwin/BUILD @@ -0,0 +1,42 @@ +load("@nix_config//:config.bzl", "SDK_UNIVERSAL_APPLE_DARWIN") +load("@rules_rust//rust:toolchain.bzl", "rust_toolchain") + +rust_toolchain( + name = "rust_toolchain", + binary_ext = "", + cargo = "@nix_rust//:cargo", + clippy_driver = "@nix_rust//:clippy_driver", + default_edition = "2021", + dylib_ext = ".so", + env = { + "SDKROOT": SDK_UNIVERSAL_APPLE_DARWIN, + }, + exec_triple = "x86_64-unknown-nixos-gnu", + extra_exec_rustc_flags = [ + "-Clinker-flavor=ld64.lld", + ], + extra_rustc_flags = [ + "-Clinker-flavor=ld64.lld", + ], + rust_doc = "@nix_rust//:rustdoc", + rust_std = "@nix_rust//:rust_std-aarch64-apple-darwin", + rustc = "@nix_rust//:rustc", + rustc_lib = "@nix_rust//:rustc_lib", + staticlib_ext = ".a", + stdlib_linkflags = [], + target_triple = "aarch64-apple-darwin", +) + +toolchain( + name = "toolchain", + exec_compatible_with = [ + "@platforms//cpu:x86_64", + "@platforms//os:nixos", + ], + target_compatible_with = [ + "@platforms//cpu:aarch64", + "@platforms//os:macos", + ], + toolchain = ":rust_toolchain", + toolchain_type = "@rules_rust//rust:toolchain_type", +) diff --git a/examples/nix_cross_compiling/bazel/toolchains/rust/aarch64-apple-ios/BUILD b/examples/nix_cross_compiling/bazel/toolchains/rust/aarch64-apple-ios/BUILD new file mode 100644 index 0000000000..ec2dc5b340 --- /dev/null +++ b/examples/nix_cross_compiling/bazel/toolchains/rust/aarch64-apple-ios/BUILD @@ -0,0 +1,42 @@ +load("@nix_config//:config.bzl", "SDK_UNIVERSAL_APPLE_IOS") +load("@rules_rust//rust:toolchain.bzl", "rust_toolchain") + +rust_toolchain( + name = "rust_toolchain", + binary_ext = "", + cargo = "@nix_rust//:cargo", + clippy_driver = "@nix_rust//:clippy_driver", + default_edition = "2021", + dylib_ext = ".so", + env = { + "SDKROOT": SDK_UNIVERSAL_APPLE_IOS, + }, + exec_triple = "x86_64-unknown-nixos-gnu", + extra_exec_rustc_flags = [ + "-Clinker-flavor=ld64.lld", + ], + extra_rustc_flags = [ + "-Clinker-flavor=ld64.lld", + ], + rust_doc = "@nix_rust//:rustdoc", + rust_std = "@nix_rust//:rust_std-aarch64-apple-ios", + rustc = "@nix_rust//:rustc", + rustc_lib = "@nix_rust//:rustc_lib", + staticlib_ext = ".a", + stdlib_linkflags = [], + target_triple = "aarch64-apple-ios", +) + +toolchain( + name = "toolchain", + exec_compatible_with = [ + "@platforms//cpu:x86_64", + "@platforms//os:nixos", + ], + target_compatible_with = [ + "@platforms//cpu:aarch64", + "@platforms//os:ios", + ], + toolchain = ":rust_toolchain", + toolchain_type = "@rules_rust//rust:toolchain_type", +) diff --git a/examples/nix_cross_compiling/bazel/toolchains/rust/aarch64-linux-android/BUILD b/examples/nix_cross_compiling/bazel/toolchains/rust/aarch64-linux-android/BUILD new file mode 100644 index 0000000000..7f04c7b1c9 --- /dev/null +++ b/examples/nix_cross_compiling/bazel/toolchains/rust/aarch64-linux-android/BUILD @@ -0,0 +1,35 @@ +load("@rules_rust//rust:toolchain.bzl", "rust_toolchain") + +rust_toolchain( + name = "rust_toolchain", + binary_ext = "", + cargo = "@nix_rust//:cargo", + clippy_driver = "@nix_rust//:clippy_driver", + default_edition = "2021", + dylib_ext = ".so", + env = {}, + exec_triple = "x86_64-unknown-nixos-gnu", + extra_exec_rustc_flags = [], + extra_rustc_flags = [], + rust_doc = "@nix_rust//:rustdoc", + rust_std = "@nix_rust//:rust_std-aarch64-linux-android", + rustc = "@nix_rust//:rustc", + rustc_lib = "@nix_rust//:rustc_lib", + staticlib_ext = ".a", + stdlib_linkflags = [], + target_triple = "aarch64-linux-android", +) + +toolchain( + name = "toolchain", + exec_compatible_with = [ + "@platforms//cpu:x86_64", + "@platforms//os:nixos", + ], + target_compatible_with = [ + "@platforms//cpu:aarch64", + "@platforms//os:android", + ], + toolchain = ":rust_toolchain", + toolchain_type = "@rules_rust//rust:toolchain_type", +) diff --git a/examples/nix_cross_compiling/bazel/toolchains/rust/aarch64-unknown-linux-gnu/BUILD b/examples/nix_cross_compiling/bazel/toolchains/rust/aarch64-unknown-linux-gnu/BUILD new file mode 100644 index 0000000000..2098bd2fae --- /dev/null +++ b/examples/nix_cross_compiling/bazel/toolchains/rust/aarch64-unknown-linux-gnu/BUILD @@ -0,0 +1,35 @@ +load("@rules_rust//rust:toolchain.bzl", "rust_toolchain") + +rust_toolchain( + name = "rust_toolchain", + binary_ext = "", + cargo = "@nix_rust//:cargo", + clippy_driver = "@nix_rust//:clippy_driver", + default_edition = "2021", + dylib_ext = ".so", + env = {}, + exec_triple = "x86_64-unknown-nixos-gnu", + extra_exec_rustc_flags = [], + extra_rustc_flags = [], + rust_doc = "@nix_rust//:rustdoc", + rust_std = "@nix_rust//:rust_std-aarch64-unknown-linux-gnu", + rustc = "@nix_rust//:rustc", + rustc_lib = "@nix_rust//:rustc_lib", + staticlib_ext = ".a", + stdlib_linkflags = [], + target_triple = "aarch64-unknown-linux-gnu", +) + +toolchain( + name = "toolchain", + exec_compatible_with = [ + "@platforms//cpu:x86_64", + "@platforms//os:nixos", + ], + target_compatible_with = [ + "@platforms//cpu:aarch64", + "@platforms//os:linux", + ], + toolchain = ":rust_toolchain", + toolchain_type = "@rules_rust//rust:toolchain_type", +) diff --git a/examples/nix_cross_compiling/bazel/toolchains/rust/wasm32-unknown-unknown/BUILD b/examples/nix_cross_compiling/bazel/toolchains/rust/wasm32-unknown-unknown/BUILD new file mode 100644 index 0000000000..d26c6e5b02 --- /dev/null +++ b/examples/nix_cross_compiling/bazel/toolchains/rust/wasm32-unknown-unknown/BUILD @@ -0,0 +1,35 @@ +load("@rules_rust//rust:toolchain.bzl", "rust_toolchain") + +rust_toolchain( + name = "rust_toolchain", + binary_ext = ".wasm", + cargo = "@nix_rust//:cargo", + clippy_driver = "@nix_rust//:clippy_driver", + default_edition = "2021", + dylib_ext = ".so", + env = {}, + exec_triple = "x86_64-unknown-nixos-gnu", + extra_exec_rustc_flags = [], + extra_rustc_flags = [], + rust_doc = "@nix_rust//:rustdoc", + rust_std = "@nix_rust//:rust_std-wasm32-unknown-unknown", + rustc = "@nix_rust//:rustc", + rustc_lib = "@nix_rust//:rustc_lib", + staticlib_ext = ".a", + stdlib_linkflags = [], + target_triple = "wasm32-unknown-unknown", +) + +toolchain( + name = "toolchain", + exec_compatible_with = [ + "@platforms//cpu:x86_64", + "@platforms//os:nixos", + ], + target_compatible_with = [ + "@platforms//cpu:wasm32", + "@platforms//os:none", + ], + toolchain = ":rust_toolchain", + toolchain_type = "@rules_rust//rust:toolchain_type", +) diff --git a/examples/nix_cross_compiling/bazel/toolchains/rust/wasm32-wasi/BUILD b/examples/nix_cross_compiling/bazel/toolchains/rust/wasm32-wasi/BUILD new file mode 100644 index 0000000000..8590c73123 --- /dev/null +++ b/examples/nix_cross_compiling/bazel/toolchains/rust/wasm32-wasi/BUILD @@ -0,0 +1,35 @@ +load("@rules_rust//rust:toolchain.bzl", "rust_toolchain") + +rust_toolchain( + name = "rust_toolchain", + binary_ext = ".wasm", + cargo = "@nix_rust//:cargo", + clippy_driver = "@nix_rust//:clippy_driver", + default_edition = "2021", + dylib_ext = ".so", + env = {}, + exec_triple = "x86_64-unknown-nixos-gnu", + extra_exec_rustc_flags = [], + extra_rustc_flags = [], + rust_doc = "@nix_rust//:rustdoc", + rust_std = "@nix_rust//:rust_std-wasm32-wasi", + rustc = "@nix_rust//:rustc", + rustc_lib = "@nix_rust//:rustc_lib", + staticlib_ext = ".a", + stdlib_linkflags = [], + target_triple = "wasm32-wasi", +) + +toolchain( + name = "toolchain", + exec_compatible_with = [ + "@platforms//cpu:x86_64", + "@platforms//os:nixos", + ], + target_compatible_with = [ + "@platforms//cpu:wasm32", + "@platforms//os:wasi", + ], + toolchain = ":rust_toolchain", + toolchain_type = "@rules_rust//rust:toolchain_type", +) diff --git a/examples/nix_cross_compiling/bazel/toolchains/rust/x86_64-apple-darwin/BUILD b/examples/nix_cross_compiling/bazel/toolchains/rust/x86_64-apple-darwin/BUILD new file mode 100644 index 0000000000..e17c2c577a --- /dev/null +++ b/examples/nix_cross_compiling/bazel/toolchains/rust/x86_64-apple-darwin/BUILD @@ -0,0 +1,42 @@ +load("@nix_config//:config.bzl", "SDK_UNIVERSAL_APPLE_DARWIN") +load("@rules_rust//rust:toolchain.bzl", "rust_toolchain") + +rust_toolchain( + name = "rust_toolchain", + binary_ext = "", + cargo = "@nix_rust//:cargo", + clippy_driver = "@nix_rust//:clippy_driver", + default_edition = "2021", + dylib_ext = ".dylib", + env = { + "SDKROOT": SDK_UNIVERSAL_APPLE_DARWIN, + }, + exec_triple = "x86_64-unknown-nixos-gnu", + extra_exec_rustc_flags = [ + "-Clinker-flavor=ld64.lld", + ], + extra_rustc_flags = [ + "-Clinker-flavor=ld64.lld", + ], + rust_doc = "@nix_rust//:rustdoc", + rust_std = "@nix_rust//:rust_std-x86_64-apple-darwin", + rustc = "@nix_rust//:rustc", + rustc_lib = "@nix_rust//:rustc_lib", + staticlib_ext = ".a", + stdlib_linkflags = [], + target_triple = "x86_64-apple-darwin", +) + +toolchain( + name = "toolchain", + exec_compatible_with = [ + "@platforms//cpu:x86_64", + "@platforms//os:nixos", + ], + target_compatible_with = [ + "@platforms//cpu:x86_64", + "@platforms//os:macos", + ], + toolchain = ":rust_toolchain", + toolchain_type = "@rules_rust//rust:toolchain_type", +) diff --git a/examples/nix_cross_compiling/bazel/toolchains/rust/x86_64-pc-windows-msvc/BUILD b/examples/nix_cross_compiling/bazel/toolchains/rust/x86_64-pc-windows-msvc/BUILD new file mode 100644 index 0000000000..8218e6404a --- /dev/null +++ b/examples/nix_cross_compiling/bazel/toolchains/rust/x86_64-pc-windows-msvc/BUILD @@ -0,0 +1,35 @@ +load("@rules_rust//rust:toolchain.bzl", "rust_toolchain") + +rust_toolchain( + name = "rust_toolchain", + binary_ext = ".exe", + cargo = "@nix_rust//:cargo", + clippy_driver = "@nix_rust//:clippy_driver", + default_edition = "2021", + dylib_ext = ".dll", + env = {}, + exec_triple = "x86_64-unknown-nixos-gnu", + extra_exec_rustc_flags = [], + extra_rustc_flags = [], + rust_doc = "@nix_rust//:rustdoc", + rust_std = "@nix_rust//:rust_std-x86_64-pc-windows-msvc", + rustc = "@nix_rust//:rustc", + rustc_lib = "@nix_rust//:rustc_lib", + staticlib_ext = ".lib", + stdlib_linkflags = [], + target_triple = "x86_64-pc-windows-msvc", +) + +toolchain( + name = "toolchain", + exec_compatible_with = [ + "@platforms//cpu:x86_64", + "@platforms//os:nixos", + ], + target_compatible_with = [ + "@platforms//cpu:x86_64", + "@platforms//os:windows", + ], + toolchain = ":rust_toolchain", + toolchain_type = "@rules_rust//rust:toolchain_type", +) diff --git a/examples/nix_cross_compiling/bazel/toolchains/rust/x86_64-unknown-linux-gnu/BUILD b/examples/nix_cross_compiling/bazel/toolchains/rust/x86_64-unknown-linux-gnu/BUILD new file mode 100644 index 0000000000..52f73ca3fb --- /dev/null +++ b/examples/nix_cross_compiling/bazel/toolchains/rust/x86_64-unknown-linux-gnu/BUILD @@ -0,0 +1,35 @@ +load("@rules_rust//rust:toolchain.bzl", "rust_toolchain") + +rust_toolchain( + name = "rust_toolchain", + binary_ext = "", + cargo = "@nix_rust//:cargo", + clippy_driver = "@nix_rust//:clippy_driver", + default_edition = "2021", + dylib_ext = ".so", + env = {}, + exec_triple = "x86_64-unknown-nixos-gnu", + extra_exec_rustc_flags = [], + extra_rustc_flags = [], + rust_doc = "@nix_rust//:rustdoc", + rust_std = "@nix_rust//:rust_std-x86_64-unknown-linux-gnu", + rustc = "@nix_rust//:rustc", + rustc_lib = "@nix_rust//:rustc_lib", + staticlib_ext = ".a", + stdlib_linkflags = [], + target_triple = "x86_64-unknown-linux-gnu", +) + +toolchain( + name = "toolchain", + exec_compatible_with = [ + "@platforms//cpu:x86_64", + "@platforms//os:nixos", + ], + target_compatible_with = [ + "@platforms//cpu:x86_64", + "@platforms//os:linux", + ], + toolchain = ":rust_toolchain", + toolchain_type = "@rules_rust//rust:toolchain_type", +) diff --git a/examples/nix_cross_compiling/bazel/toolchains/rust/x86_64-unknown-nixos-gnu/BUILD b/examples/nix_cross_compiling/bazel/toolchains/rust/x86_64-unknown-nixos-gnu/BUILD new file mode 100644 index 0000000000..b679ab1777 --- /dev/null +++ b/examples/nix_cross_compiling/bazel/toolchains/rust/x86_64-unknown-nixos-gnu/BUILD @@ -0,0 +1,35 @@ +load("@rules_rust//rust:toolchain.bzl", "rust_toolchain") + +rust_toolchain( + name = "rust_toolchain", + binary_ext = "", + cargo = "@nix_rust//:cargo", + clippy_driver = "@nix_rust//:clippy_driver", + default_edition = "2021", + dylib_ext = ".so", + env = {}, + exec_triple = "x86_64-unknown-nixos-gnu", + extra_exec_rustc_flags = [], + extra_rustc_flags = [], + rust_doc = "@nix_rust//:rustdoc", + rust_std = "@nix_rust//:rust_std-x86_64-unknown-linux-gnu", + rustc = "@nix_rust//:rustc", + rustc_lib = "@nix_rust//:rustc_lib", + staticlib_ext = ".a", + stdlib_linkflags = [], + target_triple = "x86_64-unknown-linux-gnu", +) + +toolchain( + name = "toolchain", + exec_compatible_with = [ + "@platforms//cpu:x86_64", + "@platforms//os:nixos", + ], + target_compatible_with = [ + "@platforms//cpu:x86_64", + "@platforms//os:nixos", + ], + toolchain = ":rust_toolchain", + toolchain_type = "@rules_rust//rust:toolchain_type", +) diff --git a/examples/nix_cross_compiling/cc_binary/BUILD b/examples/nix_cross_compiling/cc_binary/BUILD new file mode 100644 index 0000000000..b0e577ef2c --- /dev/null +++ b/examples/nix_cross_compiling/cc_binary/BUILD @@ -0,0 +1,15 @@ +package(default_visibility = ["//visibility:public"]) + +cc_binary( + name = "cc_binary", + srcs = select({ + # See comment in `cc_binary_wasm32-unknown-unknown.cc`. + "@platforms//os:none": ["cc_binary_wasm32-unknown-unknown.cc"], + "//conditions:default": ["cc_binary.cc"], + }), + tags = ["platform_missing"], + deps = [ + "//cc_library", + "//rust_library:rust_library_cc", + ], +) diff --git a/examples/nix_cross_compiling/cc_binary/cc_binary.cc b/examples/nix_cross_compiling/cc_binary/cc_binary.cc new file mode 100644 index 0000000000..baee0bc515 --- /dev/null +++ b/examples/nix_cross_compiling/cc_binary/cc_binary.cc @@ -0,0 +1,12 @@ +#include +#include + +extern "C" int cc_double_value(int value); +extern "C" int rust_double_value(int value); + +int main() { + printf("Hello World!\n"); + std::cout << "CC: " << cc_double_value(5) << std::endl; + std::cout << "Rust: " << rust_double_value(5) << std::endl; + return 0; +} diff --git a/examples/nix_cross_compiling/cc_binary/cc_binary_wasm32-unknown-unknown.cc b/examples/nix_cross_compiling/cc_binary/cc_binary_wasm32-unknown-unknown.cc new file mode 100644 index 0000000000..2a1959e548 --- /dev/null +++ b/examples/nix_cross_compiling/cc_binary/cc_binary_wasm32-unknown-unknown.cc @@ -0,0 +1,10 @@ +extern "C" int cc_double_value(int value); +extern "C" int rust_double_value(int value); + +// `wasm32-unknown-unknown` is very barebones, so we have to define `_start()` +// and we don't have access to `stdio.h` or `iostream`, so we can't output +// anything, but we can still call the functions. +extern "C" void _start() { + cc_double_value(5); + rust_double_value(5); +} diff --git a/examples/nix_cross_compiling/cc_library/BUILD b/examples/nix_cross_compiling/cc_library/BUILD new file mode 100644 index 0000000000..6860c546b3 --- /dev/null +++ b/examples/nix_cross_compiling/cc_library/BUILD @@ -0,0 +1,9 @@ +package(default_visibility = ["//visibility:public"]) + +cc_library( + name = "cc_library", + srcs = ["cc_library.cc"], + linkstatic = True, + tags = ["platform_missing"], + deps = [], +) diff --git a/examples/nix_cross_compiling/cc_library/cc_library.cc b/examples/nix_cross_compiling/cc_library/cc_library.cc new file mode 100644 index 0000000000..55bc481179 --- /dev/null +++ b/examples/nix_cross_compiling/cc_library/cc_library.cc @@ -0,0 +1,3 @@ +extern "C" int cc_double_value(int value) { + return value * 2; +} diff --git a/examples/nix_cross_compiling/flake.lock b/examples/nix_cross_compiling/flake.lock new file mode 100644 index 0000000000..235a1807e6 --- /dev/null +++ b/examples/nix_cross_compiling/flake.lock @@ -0,0 +1,159 @@ +{ + "nodes": { + "android-nixpkgs": { + "inputs": { + "devshell": "devshell", + "flake-utils": "flake-utils", + "nixpkgs": [ + "nixpkgs" + ] + }, + "locked": { + "lastModified": 1685650754, + "narHash": "sha256-j5Uab0FkiXjW5xi5qNV/Y5G16yj+n9IaPB89QFcrq6c=", + "owner": "tadfisher", + "repo": "android-nixpkgs", + "rev": "0a1f5cc7cb04f439eb2c6ccb1355bffe253d42b5", + "type": "github" + }, + "original": { + "owner": "tadfisher", + "repo": "android-nixpkgs", + "type": "github" + } + }, + "devshell": { + "inputs": { + "nixpkgs": [ + "android-nixpkgs", + "nixpkgs" + ], + "systems": "systems" + }, + "locked": { + "lastModified": 1683635384, + "narHash": "sha256-9goJTd05yOyD/McaMqZ4BUB8JW+mZMnZQJZ7VQ6C/Lw=", + "owner": "numtide", + "repo": "devshell", + "rev": "5143ea68647c4cf5227e4ad2100db6671fc4c369", + "type": "github" + }, + "original": { + "owner": "numtide", + "repo": "devshell", + "type": "github" + } + }, + "fenix": { + "inputs": { + "nixpkgs": [ + "nixpkgs" + ], + "rust-analyzer-src": "rust-analyzer-src" + }, + "locked": { + "lastModified": 1685600533, + "narHash": "sha256-7oly5/7xJMtFH44I/Bsrnc2VG4M6HyUVhph3WoZrG64=", + "owner": "nix-community", + "repo": "fenix", + "rev": "360c6a0bc9b78b896fdd60c8803ba98298a37f7c", + "type": "github" + }, + "original": { + "owner": "nix-community", + "repo": "fenix", + "type": "github" + } + }, + "flake-utils": { + "inputs": { + "systems": "systems_2" + }, + "locked": { + "lastModified": 1685518550, + "narHash": "sha256-o2d0KcvaXzTrPRIo0kOLV0/QXHhDQ5DTi+OxcjO8xqY=", + "owner": "numtide", + "repo": "flake-utils", + "rev": "a1720a10a6cfe8234c0e93907ffe81be440f4cef", + "type": "github" + }, + "original": { + "owner": "numtide", + "repo": "flake-utils", + "type": "github" + } + }, + "nixpkgs": { + "locked": { + "lastModified": 1685564631, + "narHash": "sha256-8ywr3AkblY4++3lIVxmrWZFzac7+f32ZEhH/A8pNscI=", + "owner": "NixOS", + "repo": "nixpkgs", + "rev": "4f53efe34b3a8877ac923b9350c874e3dcd5dc0a", + "type": "github" + }, + "original": { + "owner": "NixOS", + "ref": "nixos-unstable", + "repo": "nixpkgs", + "type": "github" + } + }, + "root": { + "inputs": { + "android-nixpkgs": "android-nixpkgs", + "fenix": "fenix", + "nixpkgs": "nixpkgs" + } + }, + "rust-analyzer-src": { + "flake": false, + "locked": { + "lastModified": 1685541053, + "narHash": "sha256-ck8hhiuxvy8jLIv2cPsQK8bfc/aYZjrIelmLAv16T4o=", + "owner": "rust-lang", + "repo": "rust-analyzer", + "rev": "bafa6c4ee5d3acdbb62ec289364564270357c6a2", + "type": "github" + }, + "original": { + "owner": "rust-lang", + "ref": "nightly", + "repo": "rust-analyzer", + "type": "github" + } + }, + "systems": { + "locked": { + "lastModified": 1681028828, + "narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=", + "owner": "nix-systems", + "repo": "default", + "rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e", + "type": "github" + }, + "original": { + "owner": "nix-systems", + "repo": "default", + "type": "github" + } + }, + "systems_2": { + "locked": { + "lastModified": 1681028828, + "narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=", + "owner": "nix-systems", + "repo": "default", + "rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e", + "type": "github" + }, + "original": { + "owner": "nix-systems", + "repo": "default", + "type": "github" + } + } + }, + "root": "root", + "version": 7 +} diff --git a/examples/nix_cross_compiling/flake.nix b/examples/nix_cross_compiling/flake.nix new file mode 100644 index 0000000000..92bbff2726 --- /dev/null +++ b/examples/nix_cross_compiling/flake.nix @@ -0,0 +1,172 @@ +{ + inputs = { + nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable"; + + fenix = { + url = "github:nix-community/fenix"; + inputs.nixpkgs.follows = "nixpkgs"; + }; + + android-nixpkgs = { + url = "github:tadfisher/android-nixpkgs"; + inputs.nixpkgs.follows = "nixpkgs"; + }; + }; + + outputs = { self, nixpkgs, fenix, android-nixpkgs, ... }: + let + pkgs = import nixpkgs { system = "x86_64-linux"; }; + + llvm = pkgs.llvmPackages_16; + + rust = with fenix.packages."x86_64-linux"; combine [ + complete.cargo + complete.clippy + complete.rustc + complete.rustfmt + complete.rust-src + complete.rust-analyzer + targets."aarch64-apple-darwin".latest.rust-std + targets."aarch64-apple-ios".latest.rust-std + targets."aarch64-linux-android".latest.rust-std + targets."aarch64-unknown-linux-gnu".latest.rust-std + targets."wasm32-unknown-unknown".latest.rust-std + targets."wasm32-wasi".latest.rust-std + targets."x86_64-apple-darwin".latest.rust-std + targets."x86_64-pc-windows-msvc".latest.rust-std + targets."x86_64-unknown-linux-gnu".latest.rust-std + ]; + + fetchVendor = { name, url, sha256, stripComponents ? 0 }: + let + authorization = + if (pkgs.lib.hasPrefix "https://raw.githubusercontent.com//" url) then + ''-H "Authorization: Bearer "'' + else + ""; + in + pkgs.runCommandLocal "vendor-${name}" + { + buildInputs = [ pkgs.cacert ]; + outputHashMode = "recursive"; + outputHashAlgo = "sha256"; + outputHash = sha256; + } '' + # Empty URL special cased for example + mkdir --parents $out + if [ -n "${url}" ]; then + ${pkgs.curl}/bin/curl -s -S -L ${authorization} "${url}" | ${pkgs.libarchive}/bin/bsdtar -C $out -xf - --strip-components ${toString stripComponents} + fi + ''; + + libclang_rt_wasm32 = fetchVendor { + name = "libclang_rt_wasm32"; + url = "https://github.com/WebAssembly/wasi-sdk/releases/download/wasi-sdk-20/libclang_rt.builtins-wasm32-wasi-20.0.tar.gz"; + sha256 = "AdRe1XrGeBuai1p5IMUTR7T7nhNlD1RZ8grZjVoHAKs="; + }; + + sdk_aarch64-unknown-linux-gnu = fetchVendor { + name = "sdk_aarch64-unknown-linux-gnu"; + url = "https://commondatastorage.googleapis.com/chrome-linux-sysroot/toolchain/80fc74e431f37f590d0c85f16a9d8709088929e8/debian_bullseye_arm64_sysroot.tar.xz"; + sha256 = "VwHx6SjTmnGWvEoevjThR2oxNEe9NIdnSIJ9RBgKPE8="; + }; + + sdk_universal-apple-darwin = fetchVendor { + name = "sdk_universal-apple-darwin"; + url = ""; # User needs to supply. + sha256 = "pQpattmS9VmO3ZIQUFn66az8GSmB4IvYhTTCFn6SUmo="; # User needs to supply. + }; + + sdk_universal-apple-ios = fetchVendor { + name = "sdk_universal-apple-ios"; + url = ""; # User needs to supply. + sha256 = "pQpattmS9VmO3ZIQUFn66az8GSmB4IvYhTTCFn6SUmo="; # User needs to supply. + }; + + sdk_universal-linux-android = android-nixpkgs.sdk."x86_64-linux" (sdkPkgs: with sdkPkgs; [ + cmdline-tools-latest + build-tools-33-0-2 + platform-tools + platforms-android-33 + ndk-25-2-9519653 + ]); + + sdk_wasm32-wasi = fetchVendor { + name = "sdk_wasm32-wasi"; + url = "https://github.com/WebAssembly/wasi-sdk/releases/download/wasi-sdk-20/wasi-sysroot-20.0.tar.gz"; + sha256 = "aePDwDYWopPPDSO802BO3YWM4d/J4a4CmGP/hDPF8FY="; + }; + + sdk_x86_64-pc-windows-msvc = fetchVendor { + name = "sdk_x86_64-pc-windows-msvc"; + url = ""; # User needs to supply. + sha256 = "pQpattmS9VmO3ZIQUFn66az8GSmB4IvYhTTCFn6SUmo="; # User needs to supply. + }; + + sdk_x86_64-unknown-linux-gnu = fetchVendor { + name = "sdk_x86_64-unknown-linux-gnu"; + url = "https://commondatastorage.googleapis.com/chrome-linux-sysroot/toolchain/f5f68713249b52b35db9e08f67184cac392369ab/debian_bullseye_amd64_sysroot.tar.xz"; + sha256 = "+pqXDAsKY1BCBGFD3PDqMv1YiU+0/B2LXXryTFIUaWk="; + }; + + sdk_x86_64-unknown-nixos-gnu = pkgs.symlinkJoin { + name = "sdk_x86_64-unknown-nixos-gnu"; + paths = [ + llvm.libcxx.out + llvm.libcxx.dev + llvm.libcxxabi.out + llvm.libunwind.out + pkgs.gcc13.cc.libgcc.out + pkgs.gcc13.cc.libgcc.libgcc + pkgs.glibc.dev + pkgs.glibc.libgcc.out + pkgs.glibc.out + ]; + }; + in + { + packages."x86_64-linux".bazel = { + config = pkgs.writeTextFile { + name = "bazel-config"; + destination = "/config.bzl"; + text = '' + LLVM = "${llvm.bintools-unwrapped}" + CLANG = "${llvm.clang-unwrapped}" + CLANG_LIB = "${llvm.clang-unwrapped.lib}" + CLANG_LIB_VERSION = "16" + + NIXOS_DYNAMIC_LINKER = "${pkgs.glibc.out}/lib64/ld-linux-x86-64.so.2" + ANDROID_NDK_VERSION = "25.2.9519653" + LIBCLANG_RT_WASM32 = "${libclang_rt_wasm32}" + + SDK_AARCH64_UNKNOWN_LINUX_GNU = "${sdk_aarch64-unknown-linux-gnu}" + SDK_UNIVERSAL_APPLE_DARWIN = "${sdk_universal-apple-darwin}" + SDK_UNIVERSAL_APPLE_IOS = "${sdk_universal-apple-ios}" + SDK_UNIVERSAL_LINUX_ANDROID = "${sdk_universal-linux-android}" + SDK_WASM32_WASI = "${sdk_wasm32-wasi}" + SDK_X86_64_PC_WINDOWS_MSVC = "${sdk_x86_64-pc-windows-msvc}" + SDK_X86_64_UNKNOWN_LINUX_GNU = "${sdk_x86_64-unknown-linux-gnu}" + SDK_X86_64_UNKNOWN_NIXOS_GNU = "${sdk_x86_64-unknown-nixos-gnu}" + ''; + }; + + rust = rust; + }; + + devShells."x86_64-linux".default = pkgs.mkShellNoCC + { + packages = [ + llvm.bintools-unwrapped + llvm.clang-unwrapped + rust + + pkgs.bazel-buildtools + pkgs.bazel_6 + pkgs.cacert + pkgs.git + pkgs.jdk19_headless # Needed for Android tools + pkgs.nix + ]; + }; + }; +} diff --git a/examples/nix_cross_compiling/rust_binary/BUILD b/examples/nix_cross_compiling/rust_binary/BUILD new file mode 100644 index 0000000000..19016580ed --- /dev/null +++ b/examples/nix_cross_compiling/rust_binary/BUILD @@ -0,0 +1,13 @@ +load("@rules_rust//rust:defs.bzl", "rust_binary") + +package(default_visibility = ["//visibility:public"]) + +rust_binary( + name = "rust_binary", + srcs = ["rust_binary.rs"], + tags = ["platform_missing"], + deps = [ + "//cc_library", + "//rust_library", + ], +) diff --git a/examples/nix_cross_compiling/rust_binary/rust_binary.rs b/examples/nix_cross_compiling/rust_binary/rust_binary.rs new file mode 100644 index 0000000000..1f80405f86 --- /dev/null +++ b/examples/nix_cross_compiling/rust_binary/rust_binary.rs @@ -0,0 +1,10 @@ +use rust_library::rust_double_value; + +extern "C" { fn cc_double_value(value: i32) -> i32; } + +fn main() +{ + println!("Hello World!"); + println!("CC: {}", unsafe{ cc_double_value(5) }); + println!("Rust: {}", rust_double_value(5)); +} diff --git a/examples/nix_cross_compiling/rust_library/BUILD b/examples/nix_cross_compiling/rust_library/BUILD new file mode 100644 index 0000000000..f7dee8cb8e --- /dev/null +++ b/examples/nix_cross_compiling/rust_library/BUILD @@ -0,0 +1,19 @@ +load("@rules_rust//rust:defs.bzl", "rust_library", "rust_static_library") + +package(default_visibility = ["//visibility:public"]) + +rust_library( + name = "rust_library", + srcs = ["rust_library.rs"], + tags = ["platform_missing"], + deps = [], +) + +# CC needs to link against a Rust `staticlib` crate which includes +# all the Rust `std` symbols. +rust_static_library( + name = "rust_library_cc", + srcs = ["rust_library.rs"], + tags = ["platform_missing"], + deps = [], +) diff --git a/examples/nix_cross_compiling/rust_library/rust_library.rs b/examples/nix_cross_compiling/rust_library/rust_library.rs new file mode 100644 index 0000000000..8fe7cfd9b3 --- /dev/null +++ b/examples/nix_cross_compiling/rust_library/rust_library.rs @@ -0,0 +1,5 @@ +#[no_mangle] +pub fn rust_double_value(value: i32) -> i32 +{ + value * 2 +}