···11+fixupOutputHooks+=('signDarwinBinariesIn $prefix')
22+33+# Uses signingUtils, see definition of autoSignDarwinBinariesHook in
44+# darwin-packages.nix
55+66+signDarwinBinariesIn() {
77+ local dir="$1"
88+99+ if [ ! -d "$dir" ]; then
1010+ return 0
1111+ fi
1212+1313+ if [ "${darwinDontCodeSign:-}" ]; then
1414+ return 0
1515+ fi
1616+1717+ while IFS= read -r -d $'\0' f; do
1818+ signIfRequired "$f"
1919+ done < <(find "$dir" -type f -print0)
2020+}
···11+# Work around for some odd behaviour where we can't codesign a file
22+# in-place if it has been called before. This happens for example if
33+# you try to fix-up a binary using strip/install_name_tool, after it
44+# had been used previous. The solution is to copy the binary (with
55+# the corrupted signature from strip/install_name_tool) to some
66+# location, sign it there and move it back into place.
77+#
88+# This does not appear to happen with the codesign tool that ships
99+# with recent macOS BigSur installs on M1 arm64 machines. However it
1010+# had also been happening with the tools that shipped with the DTKs.
1111+sign() {
1212+ local tmpdir
1313+ tmpdir=$(mktemp -d)
1414+1515+ # $1 is the file
1616+1717+ cp "$1" "$tmpdir"
1818+ CODESIGN_ALLOCATE=@codesignAllocate@ \
1919+ @sigtool@/bin/codesign -f -s - "$tmpdir/$(basename "$1")"
2020+ mv "$tmpdir/$(basename "$1")" "$1"
2121+ rmdir "$tmpdir"
2222+}
2323+2424+checkRequiresSignature() {
2525+ local file=$1
2626+ local rc=0
2727+2828+ @sigtool@/bin/sigtool --file "$file" check-requires-signature || rc=$?
2929+3030+ if [ "$rc" -eq 0 ] || [ "$rc" -eq 1 ]; then
3131+ return "$rc"
3232+ fi
3333+3434+ echo "Unexpected exit status from sigtool: $rc"
3535+ exit 1
3636+}
3737+3838+signIfRequired() {
3939+ local file=$1
4040+ if checkRequiresSignature "$file"; then
4141+ sign "$file"
4242+ fi
4343+}