@@ -83,9 +83,10 @@ WORKSPACE = "{:workspace:}"
83
83
CC = os .environ .get ("CC_WRAPPER_CC_PATH" , "{:cc:}" )
84
84
PLATFORM = os .environ .get ("CC_WRAPPER_PLATFORM" , "{:platform:}" )
85
85
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"
89
90
90
91
91
92
def main ():
@@ -930,12 +931,18 @@ def darwin_rewrite_load_commands(rewrites, output):
930
931
if args :
931
932
subprocess .check_call ([INSTALL_NAME_TOOL ] + args + [output ])
932
933
# 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"
933
940
# This is necessary on MacOS Monterey on M1.
934
941
# The moving back and forth is necessary because the OS caches the signature.
935
942
# See this note from nixpkgs for reference:
936
943
# https://github.com/NixOS/nixpkgs/blob/5855ff74f511423e3e2646248598b3ffff229223/pkgs/os-specific/darwin/signing-utils/utils.sh#L1-L6
937
944
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 } )
939
946
os .rename (f"{ output } .resign" , output )
940
947
941
948
0 commit comments