Skip to content

Commit ff807d9

Browse files
authored
Merge pull request #2136 from tweag/darwin-dont-use-system-tools
Do not depend on system tools on Darwin
2 parents 18577ae + ac40163 commit ff807d9

File tree

2 files changed

+17
-4
lines changed

2 files changed

+17
-4
lines changed

haskell/private/cc_wrapper.bzl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
load("@bazel_skylib//lib:paths.bzl", "paths")
12
load("@rules_cc//cc:action_names.bzl", "ACTION_NAMES")
23
load("@rules_cc//cc:find_cc_toolchain.bzl", "find_cc_toolchain", "use_cc_toolchain")
34
load("@rules_python//python:defs.bzl", "py_binary")
@@ -46,6 +47,10 @@ def _cc_wrapper_impl(ctx):
4647
feature_configuration = feature_configuration,
4748
action_name = ACTION_NAMES.c_compile,
4849
)
50+
ar = cc_common.get_tool_for_action(
51+
feature_configuration = feature_configuration,
52+
action_name = ACTION_NAMES.cpp_link_static_library,
53+
)
4954
cc_wrapper = ctx.actions.declare_file(ctx.label.name)
5055
ctx.actions.expand_template(
5156
template = ctx.file.template,
@@ -56,6 +61,7 @@ def _cc_wrapper_impl(ctx):
5661
"{:cpu:}": cc_toolchain.cpu,
5762
"{:workspace:}": ctx.workspace_name,
5863
"{:platform:}": ctx.attr.platform,
64+
"{:bindir:}": paths.dirname(ar),
5965
},
6066
)
6167
return [DefaultInfo(

haskell/private/cc_wrapper.py.tpl

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,10 @@ WORKSPACE = "{:workspace:}"
8383
CC = os.environ.get("CC_WRAPPER_CC_PATH", "{:cc:}")
8484
PLATFORM = os.environ.get("CC_WRAPPER_PLATFORM", "{:platform:}")
8585
CPU = os.environ.get("CC_WRAPPER_CPU", "{:cpu:}")
86-
INSTALL_NAME_TOOL = "/usr/bin/install_name_tool"
87-
CODESIGN = "/usr/bin/codesign"
88-
OTOOL = "/usr/bin/otool"
86+
INSTALL_NAME_TOOL = "{:bindir:}/install_name_tool"
87+
CODESIGN = "{:bindir:}/codesign"
88+
CODESIGN_ALLOCATE = "{:bindir:}/codesign_allocate"
89+
OTOOL = "{:bindir:}/otool"
8990

9091

9192
def main():
@@ -930,12 +931,18 @@ def darwin_rewrite_load_commands(rewrites, output):
930931
if args:
931932
subprocess.check_call([INSTALL_NAME_TOOL] + args + [output])
932933
# Resign the binary after patching it.
934+
# Fall back to /usr/bin/codesign if the `CODESIGN` executable is not available
935+
# (this might happen when using a default cc toolchain from a nix shell on Darwin instead
936+
# of using a nixpkgs_cc_configure'd toolchain).
937+
# Do the same for codesign_allocate.
938+
codesign = CODESIGN if os.access(CODESIGN, os.X_OK) else "/usr/bin/codesign"
939+
codesign_allocate = CODESIGN_ALLOCATE if os.access(CODESIGN_ALLOCATE, os.X_OK) else "/usr/bin/codesign_allocate"
933940
# This is necessary on MacOS Monterey on M1.
934941
# The moving back and forth is necessary because the OS caches the signature.
935942
# See this note from nixpkgs for reference:
936943
# https://github.com/NixOS/nixpkgs/blob/5855ff74f511423e3e2646248598b3ffff229223/pkgs/os-specific/darwin/signing-utils/utils.sh#L1-L6
937944
os.rename(output, f"{output}.resign")
938-
subprocess.check_call([CODESIGN] + ["-f", "-s", "-"] + [f"{output}.resign"])
945+
subprocess.check_call([codesign] + ["-f", "-s", "-"] + [f"{output}.resign"], env = {'CODESIGN_ALLOCATE': codesign_allocate})
939946
os.rename(f"{output}.resign", output)
940947

941948

0 commit comments

Comments
 (0)