bazel: add cross compiler for darwin

This allows cross compiling from aarch64-darwin to x86_64-linux.
It is required for building Go binaries on macos that target Linux and have CGO enabled.
This commit is contained in:
Malte Poll 2023-11-29 18:43:21 +01:00
parent bd3430fcf0
commit 5c19b2c27b
4 changed files with 92 additions and 5 deletions

View File

@ -17,8 +17,8 @@ load("@io_tweag_rules_nixpkgs//nixpkgs:nixpkgs.bzl", "nixpkgs_cc_configure", "ni
nixpkgs_git_repository( nixpkgs_git_repository(
name = "nixpkgs", name = "nixpkgs",
revision = "ec750fd01963ab6b20ee1f0cb488754e8036d89d", revision = "85306ef2470ba705c97ce72741d56e42d0264015",
sha256 = "9e809097a0c66334139cda3d1bec3bf73bdfad593e954259ff329c7b4a118041", sha256 = "adbbcfd49b5180e51e2971626cafb14123e3ec06c18fa143b1f386b029081f12",
) )
nixpkgs_flake_package( nixpkgs_flake_package(
@ -185,9 +185,33 @@ zig_toolchains()
nixpkgs_cc_configure( nixpkgs_cc_configure(
name = "nixpkgs_cc_toolchain", name = "nixpkgs_cc_toolchain",
# TODO(malt3): Use clang once cc-wrapper path reset bug is fixed upstream.
# attribute_path = "clang_11",
repository = "@nixpkgs", repository = "@nixpkgs",
) )
nixpkgs_cc_configure(
name = "nixpkgs_cc_aarch64_darwin_x86_64_linux",
cross_cpu = "k8",
exec_constraints = [
"@platforms//os:osx",
"@platforms//cpu:arm64",
],
nix_file = "//nix/toolchains:cc_cross_darwin_x86_64_linux.nix",
nixopts = [
"--arg",
"ccPkgs",
"import <nixpkgs> { crossSystem = \"x86_64-linux\";}",
"--show-trace",
],
repository = "@nixpkgs",
target_constraints = [
"@platforms//cpu:x86_64",
"@platforms//os:linux",
"@rules_nixpkgs_core//constraints:support_nix",
],
)
register_toolchains( register_toolchains(
"@zig_sdk//libc_aware/toolchain:linux_amd64_gnu.2.23", "@zig_sdk//libc_aware/toolchain:linux_amd64_gnu.2.23",
"@zig_sdk//libc_aware/toolchain:linux_arm64_gnu.2.23", "@zig_sdk//libc_aware/toolchain:linux_arm64_gnu.2.23",

6
flake.lock generated
View File

@ -20,11 +20,11 @@
}, },
"nixpkgsUnstable": { "nixpkgsUnstable": {
"locked": { "locked": {
"lastModified": 1699343069, "lastModified": 1701237617,
"narHash": "sha256-s7BBhyLA6MI6FuJgs4F/SgpntHBzz40/qV0xLPW6A1Q=", "narHash": "sha256-Ryd8xpNDY9MJnBFDYhB37XSFIxCPVVVXAbInNPa95vs=",
"owner": "NixOS", "owner": "NixOS",
"repo": "nixpkgs", "repo": "nixpkgs",
"rev": "ec750fd01963ab6b20ee1f0cb488754e8036d89d", "rev": "85306ef2470ba705c97ce72741d56e42d0264015",
"type": "github" "type": "github"
}, },
"original": { "original": {

View File

View File

@ -0,0 +1,63 @@
# cross compiler toolchain for cc rules in Bazel
# execution platform: aarch64-darwin
# target platform: x86_64-linux
# inspired by https://github.com/tweag/rules_nixpkgs/blob/21c4ea481021cb51a6e5d0969b2cee03dba5a637/examples/toolchains/cc_cross_osx_to_linux_amd64/toolchains/osxcross_cc.nix
let
targetSystem = "x86_64-linux";
og = import <nixpkgs> { };
nixpkgs = import <nixpkgs> {
buildSystem = builtins.currentSystem;
hostSystem = targetSystem;
crossSystem = {
config = targetSystem;
};
crossOverlays = [
(self: super: {
llvmPackages_11 = super.llvmPackages_11.extend (final: prev: rec {
libllvm = prev.libllvm.overrideAttrs (old: {
LDFLAGS = "-L ${super.llvmPackages_11.libcxxabi}/lib";
nativeBuildInputs = (old.nativeBuildInputs or [ ]) ++ [ og.darwin.cctools ];
});
libclang = prev.libclang.override {
inherit libllvm;
};
libraries = super.llvmPackages_11.libraries;
});
})
];
};
pkgsLinux = import <nixpkgs> {
config = { };
overlays = [ ];
system = targetSystem;
};
in
let
pkgs = nixpkgs.buildPackages;
linuxCC = pkgs.wrapCCWith rec {
cc = pkgs.llvmPackages_11.clang-unwrapped;
bintools = pkgs.llvmPackages_11.bintools;
extraPackages = [ pkgsLinux.glibc.static pkgs.llvmPackages_11.libraries.libcxxabi pkgs.llvmPackages_11.libraries.libcxx ];
extraBuildCommands = ''
echo "-isystem ${pkgs.llvmPackages_11.clang-unwrapped.lib}/lib/clang/${cc.version}/include" >> $out/nix-support/cc-cflags
echo "-isystem ${pkgsLinux.glibc.dev}/include" >> $out/nix-support/cc-cflags
echo "-L ${pkgs.llvmPackages_11.libraries.libcxxabi}/lib" >> $out/nix-support/cc-ldflags
echo "-L ${pkgsLinux.glibc.static}/lib" >> $out/nix-support/cc-ldflags
echo "-resource-dir=${cc}/resource-root" >> $out/nix-support/cc-cflags
'';
};
in
pkgs.buildEnv (
let
cc = linuxCC;
in
{
name = "bazel-${cc.name}-cc";
paths = [ cc cc.bintools ];
pathsToLink = [ "/bin" ];
passthru = {
inherit (cc) isClang targetPrefix;
orignalName = cc.name;
};
}
)