Skip to content

Commit cf76139

Browse files
committed
hack/toolexec-for-codesign.sh: add a wrapper script for -toolexec to codesign a executable produced by linker
By passing `-toolexec hack/toolexec-for-codesign.sh` to the Go build options, the executable will be `codesign`ed with `vz.entitlements` after linking. This eliminates the need to prepare a pre-signed debug binary when running `dlv dap` (in VSCode, for example) for debugging. usage in `launch.json`: ```json { "version": "0.2.0", "configurations": [ { "name": "Debug limactl hostagent for debug instance", "type": "go", "request": "launch", "mode": "debug", "program": "${workspaceFolder}/cmd/limactl", "buildFlags": [ "-toolexec", "${workspaceFolder}/hack/toolexec-for-codesign.sh", ], "env": { "CGO_ENABLED": "1" }, "cwd": "${userHome}/.lima/debug", "args": [ "hostagent", "--pidfile", "ha.pid", "--socket", "ha.sock", "debug" ], "preLaunchTask": "prepare launching hostagent for debug instance", "postDebugTask": "clean up after stopping hostagent for debug instance" }, ] } ``` Signed-off-by: Norio Nomura <[email protected]>
1 parent 996b340 commit cf76139

File tree

1 file changed

+54
-0
lines changed

1 file changed

+54
-0
lines changed

hack/toolexec-for-codesign.sh

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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

Comments
 (0)