Skip to content

Commit

Permalink
Patch cc-wrapper to replace --tmpdir flag
Browse files Browse the repository at this point in the history
  • Loading branch information
aherrmann committed Oct 2, 2023
1 parent c630600 commit 925f8e4
Showing 1 changed file with 62 additions and 0 deletions.
62 changes: 62 additions & 0 deletions testing/go-bzlmod/tests/nixpkgs_repositories.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,67 @@ load("@rules_nixpkgs_cc//:cc.bzl", "nixpkgs_cc_configure")
load("@rules_nixpkgs_java//:java.bzl", "nixpkgs_java_configure")
load("@rules_nixpkgs_go//:go.bzl", "nixpkgs_go_configure")

_PATCHED_CC = """\
let
ccPkgs = import <nixpkgs> { config = { }; overlays = [ ]; };
pkgs = ccPkgs.buildPackages;
stdenv = ccPkgs.stdenv;
# The original `postLinkSignHook` from nixpkgs assumes `codesign_allocate` is
# in the PATH which is not the case when using our cc_wrapper. Set
# `CODESIGN_ALLOCATE` to an absolute path here and override the hook for
# `darwinCC` below.
postLinkSignHook =
with pkgs; writeTextFile {
name = "post-link-sign-hook";
executable = true;
text = ''
CODESIGN_ALLOCATE=$${darwin.cctools}/bin/codesign_allocate \
$${darwin.sigtool}/bin/codesign -f -s - "$$linkerOutput"
'';
};
darwinCC =
# Work around https://github.com/NixOS/nixpkgs/issues/42059.
# See also https://github.com/NixOS/nixpkgs/pull/41589.
pkgs.wrapCCWith rec {
cc = stdenv.cc.cc;
bintools = stdenv.cc.bintools.override { inherit postLinkSignHook; };
extraBuildCommands = with pkgs.darwin.apple_sdk.frameworks; ''
sed -i.bak 's#--tmpdir cc-params.XXXXXX#"''$${TMPDIR:-/tmp}/cc-params.XXXXXX"#' $$out/bin/cc
echo "-Wno-unused-command-line-argument" >> $$out/nix-support/cc-cflags
echo "-Wno-elaborated-enum-base" >> $$out/nix-support/cc-cflags
echo "-isystem $${pkgs.llvmPackages.libcxx.dev}/include/c++/v1" >> $$out/nix-support/cc-cflags
echo "-isystem $${pkgs.llvmPackages.clang-unwrapped.lib}/lib/clang/$${cc.version}/include" >> $$out/nix-support/cc-cflags
echo "-F$${CoreFoundation}/Library/Frameworks" >> $$out/nix-support/cc-cflags
echo "-F$${CoreServices}/Library/Frameworks" >> $$out/nix-support/cc-cflags
echo "-F$${Security}/Library/Frameworks" >> $$out/nix-support/cc-cflags
echo "-F$${Foundation}/Library/Frameworks" >> $$out/nix-support/cc-cflags
echo "-L$${pkgs.llvmPackages.libcxx}/lib" >> $$out/nix-support/cc-cflags
echo "-L$${pkgs.llvmPackages.libcxxabi}/lib" >> $$out/nix-support/cc-cflags
echo "-L$${pkgs.libiconv}/lib" >> $$out/nix-support/cc-cflags
echo "-L$${pkgs.darwin.libobjc}/lib" >> $$out/nix-support/cc-cflags
echo "-resource-dir=$${pkgs.stdenv.cc}/resource-root" >> $$out/nix-support/cc-cflags
'';
};
in
pkgs.buildEnv (
let
cc = if stdenv.isDarwin then darwinCC else stdenv.cc;
in
{
name = "bazel-$${cc.name}-wrapper";
# XXX: `gcov` is missing in `/bin`.
# It exists in `stdenv.cc.cc` but that collides with `stdenv.cc`.
paths = [ cc cc.bintools ] ++ pkgs.lib.optional pkgs.stdenv.isDarwin pkgs.darwin.cctools;
pathsToLink = [ "/bin" ];
passthru = {
inherit (cc) isClang targetPrefix;
orignalName = cc.name;
};
}
)
"""

def nixpkgs_repositories(*, bzlmod):
nixpkgs_local_repository(
name = "nixpkgs",
Expand All @@ -14,6 +75,7 @@ def nixpkgs_repositories(*, bzlmod):
name = "nixpkgs_config_cc",
repository = "@nixpkgs",
register = not bzlmod,
nix_file_content = _PATCHED_CC,
)

nixpkgs_java_configure(
Expand Down

0 comments on commit 925f8e4

Please sign in to comment.