|
| 1 | +#!/bin/sh |
| 2 | + |
| 3 | +# This script is used to wrap the compiler and linker commands in the build |
| 4 | +# process. It captures the output of the command and logs it to a file. |
| 5 | +# The script's primary purpose is codesigning the output of the linker command |
| 6 | +# with the entitlements file if it exists. |
| 7 | +# If the OS is macOS, the result of the command is 0, the entitlements file |
| 8 | +# exists, and codesign is available, sign the output of the linker command with |
| 9 | +# the entitlements file. |
| 10 | +# |
| 11 | +# Usage: |
| 12 | +# go build -toolexec hack/toolexec-to-codesign.sh |
| 13 | + |
| 14 | +repository_root="$(dirname "$(dirname "$0")")" |
| 15 | +logfile="${repository_root}/.toolexec-to-codesign.log" |
| 16 | + |
| 17 | +echo $$: cmd: "$@" >>"${logfile}" |
| 18 | + |
| 19 | +output="$("$@")" |
| 20 | +result=$? |
| 21 | + |
| 22 | +echo $$: output: "${output}" >>"${logfile}" |
| 23 | + |
| 24 | +entitlements="${repository_root}/vz.entitlements" |
| 25 | + |
| 26 | +# If the OS is macOS, the result of the command is 0, the entitlements file |
| 27 | +# exists, and codesign is available, sign the output of the linker command. |
| 28 | +if OS=$(uname -s) && [ "${OS}" = "Darwin" ] && [ "${result}" -eq 0 ] && [ -f "${entitlements}" ] && command -v codesign >/dev/null 2>&1; then |
| 29 | + # Check if the command is a linker command. |
| 30 | + case "$1" in |
| 31 | + *link) |
| 32 | + shift |
| 33 | + # Find a parameter that is a output file. |
| 34 | + while [ $# -gt 1 ]; do |
| 35 | + case "$1" in |
| 36 | + -o) |
| 37 | + # If the output file is a executable, sign it with the entitlements file. |
| 38 | + if [ -x "$2" ]; then |
| 39 | + codesign_output="$(codesign -v --entitlements "${entitlements}" -s - "$2" 2>&1)" |
| 40 | + echo "$$: ${codesign_output}" >>"${logfile}" |
| 41 | + fi |
| 42 | + break |
| 43 | + ;; |
| 44 | + *) shift ;; |
| 45 | + esac |
| 46 | + done |
| 47 | + ;; |
| 48 | + *) ;; |
| 49 | + esac |
| 50 | +fi |
| 51 | + |
| 52 | +# Print the output of the command and exit with the result of the command. |
| 53 | +echo "${output}" |
| 54 | +exit "${result}" |
0 commit comments