diff --git a/WORKSPACE.bazel b/WORKSPACE.bazel index bfc7e7365..3f25dfb6a 100644 --- a/WORKSPACE.bazel +++ b/WORKSPACE.bazel @@ -17,8 +17,8 @@ load("@io_tweag_rules_nixpkgs//nixpkgs:nixpkgs.bzl", "nixpkgs_cc_configure", "ni nixpkgs_git_repository( name = "nixpkgs", - revision = "ec750fd01963ab6b20ee1f0cb488754e8036d89d", - sha256 = "9e809097a0c66334139cda3d1bec3bf73bdfad593e954259ff329c7b4a118041", + revision = "85306ef2470ba705c97ce72741d56e42d0264015", + sha256 = "adbbcfd49b5180e51e2971626cafb14123e3ec06c18fa143b1f386b029081f12", ) nixpkgs_flake_package( @@ -185,9 +185,33 @@ zig_toolchains() nixpkgs_cc_configure( name = "nixpkgs_cc_toolchain", + # TODO(malt3): Use clang once cc-wrapper path reset bug is fixed upstream. + # attribute_path = "clang_11", 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 { 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( "@zig_sdk//libc_aware/toolchain:linux_amd64_gnu.2.23", "@zig_sdk//libc_aware/toolchain:linux_arm64_gnu.2.23", diff --git a/flake.lock b/flake.lock index 618cc8592..64d89f553 100644 --- a/flake.lock +++ b/flake.lock @@ -20,11 +20,11 @@ }, "nixpkgsUnstable": { "locked": { - "lastModified": 1699343069, - "narHash": "sha256-s7BBhyLA6MI6FuJgs4F/SgpntHBzz40/qV0xLPW6A1Q=", + "lastModified": 1701237617, + "narHash": "sha256-Ryd8xpNDY9MJnBFDYhB37XSFIxCPVVVXAbInNPa95vs=", "owner": "NixOS", "repo": "nixpkgs", - "rev": "ec750fd01963ab6b20ee1f0cb488754e8036d89d", + "rev": "85306ef2470ba705c97ce72741d56e42d0264015", "type": "github" }, "original": { diff --git a/nix/toolchains/BUILD.bazel b/nix/toolchains/BUILD.bazel new file mode 100644 index 000000000..e69de29bb diff --git a/nix/toolchains/cc_cross_darwin_x86_64_linux.nix b/nix/toolchains/cc_cross_darwin_x86_64_linux.nix new file mode 100644 index 000000000..1394baeff --- /dev/null +++ b/nix/toolchains/cc_cross_darwin_x86_64_linux.nix @@ -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 = import { + 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 { + 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; + }; + } +)