Skip to content

Commit f779fea

Browse files
committed
Makefile: add code signing step for MacOS
Add a new script 'codesign.sh' to sign the contents of a given MacOS directory with the appropriate Apple Developer ID certificate (assumed to already be set up in a keychain). Add a 'codesign' target to the 'make package' workflow that only runs if the 'APPLE_APP_IDENTITY' build variable is set. Additionally, add an '--identity' argument to 'pack.sh' to sign the .pkg generated by 'productbuild' - if 'APPLE_INST_IDENTITY' is not set, the .pkg is not signed. Note that the signing identities for 'codesign.sh' and 'pack.sh' are different variables: 'APPLE_APP_IDENTITY' is the application signing identity, and 'APPLE_INST_IDENTITY' is the installer signing identity. More information on this signing process can be found at [1] and [2]. Finally, change the temporary package name suffix from '.tmp' to '.component' to more clearly indicate that it is a component package. [1] https://developer.apple.com/forums/thread/701514 [2] https://developer.apple.com/forums/thread/701581 Signed-off-by: Victoria Dye <[email protected]>
1 parent 6c259ad commit f779fea

File tree

3 files changed

+93
-2
lines changed

3 files changed

+93
-2
lines changed

Makefile

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ GOARCH := $(shell go env GOARCH)
2222
SUPPORTED_PACKAGE_GOARCHES := amd64 arm64
2323
PACKAGE_ARCH := $(GOARCH)
2424

25+
# Guard against environment variables
26+
APPLE_APP_IDENTITY =
27+
APPLE_INST_IDENTITY =
28+
2529
# Build targets
2630
.PHONY: build
2731
build:
@@ -95,7 +99,8 @@ else ifeq ($(GOOS),darwin)
9599
# Steps:
96100
# 1. Layout files in _dist/pkg/payload/ as they'll be installed (including
97101
# uninstall.sh script).
98-
# 2. Create the product archive in _dist/.
102+
# 2. (Optional) Codesign the package contents in place.
103+
# 3. Create the product archive in _dist/.
99104

100105
# Platform-specific variables
101106
PKGDIR := $(DISTDIR)/pkg
@@ -110,11 +115,24 @@ $(PKGDIR)/payload: check-arch build doc
110115
--uninstaller="$(CURDIR)/scripts/uninstall.sh" \
111116
--install-root="$(PKGDIR)/payload"
112117

118+
ifdef APPLE_APP_IDENTITY
119+
.PHONY: codesign
120+
codesign: $(PKGDIR)/payload
121+
@echo
122+
@echo "======== Codesigning package contents ========"
123+
@build/package/pkg/codesign.sh --payload="$(PKGDIR)/payload" \
124+
--identity="$(APPLE_APP_IDENTITY)" \
125+
--entitlements="$(CURDIR)/build/package/pkg/entitlements.xml"
126+
127+
$(PKG_FILENAME): codesign
128+
endif
129+
113130
$(PKG_FILENAME): check-version $(PKGDIR)/payload
114131
@echo
115132
@echo "======== Creating product archive package ========"
116133
@build/package/pkg/pack.sh --version="$(VERSION)" \
117134
--payload="$(PKGDIR)/payload" \
135+
--identity="$(APPLE_INST_IDENTITY)" \
118136
--output="$(PKG_FILENAME)"
119137

120138
.PHONY: package

build/package/pkg/codesign.sh

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
#!/bin/bash
2+
3+
sign_directory () {
4+
(
5+
cd $1
6+
for f in *
7+
do
8+
macho=$(file --mime $f | grep mach)
9+
# Runtime sign dylibs and Mach-O binaries
10+
if [[ $f == *.dylib ]] || [ ! -z "$macho" ];
11+
then
12+
echo "Runtime Signing $f"
13+
codesign -s "$IDENTITY" $f --timestamp --force --options=runtime --entitlements $ENTITLEMENTS_FILE
14+
elif [ -d "$f" ];
15+
then
16+
echo "Signing files in subdirectory $f"
17+
sign_directory $f
18+
19+
else
20+
echo "Signing $f"
21+
codesign -s "$IDENTITY" $f --timestamp --force
22+
fi
23+
done
24+
)
25+
}
26+
27+
for i in "$@"
28+
do
29+
case "$i" in
30+
--payload=*)
31+
SIGN_DIR="${i#*=}"
32+
shift # past argument=value
33+
;;
34+
--identity=*)
35+
IDENTITY="${i#*=}"
36+
shift # past argument=value
37+
;;
38+
--entitlements=*)
39+
ENTITLEMENTS_FILE="${i#*=}"
40+
shift # past argument=value
41+
;;
42+
*)
43+
die "unknown option '$i'"
44+
;;
45+
esac
46+
done
47+
48+
if [ -z "$SIGN_DIR" ]; then
49+
echo "error: missing directory argument"
50+
exit 1
51+
elif [ -z "$IDENTITY" ]; then
52+
echo "error: missing signing identity argument"
53+
exit 1
54+
elif [ -z "$ENTITLEMENTS_FILE" ]; then
55+
echo "error: missing entitlements file argument"
56+
exit 1
57+
fi
58+
59+
echo "======== INPUTS ========"
60+
echo "Directory: $SIGN_DIR"
61+
echo "Signing identity: $IDENTITY"
62+
echo "Entitlements: $ENTITLEMENTS_FILE"
63+
echo "======== END INPUTS ========"
64+
65+
sign_directory "$SIGN_DIR"

build/package/pkg/pack.sh

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ THISDIR="$( cd "$(dirname "$0")" ; pwd -P )"
1111
IDENTIFIER="com.github.gitbundleserver"
1212
INSTALL_LOCATION="/usr/local/git-bundle-server"
1313

14+
# Defaults
15+
IDENTITY=
16+
1417
# Parse script arguments
1518
for i in "$@"
1619
do
@@ -23,6 +26,10 @@ case "$i" in
2326
PAYLOAD="${i#*=}"
2427
shift # past argument=value
2528
;;
29+
--identity=*)
30+
IDENTITY="${i#*=}"
31+
shift # past argument=value
32+
;;
2633
--output=*)
2734
PKGOUT="${i#*=}"
2835
shift # past argument=value
@@ -59,7 +66,7 @@ fi
5966
mkdir -p "$(dirname "$PKGOUT")"
6067

6168
# Build the component package
62-
PKGTMP="$PKGOUT.tmp"
69+
PKGTMP="$PKGOUT.component"
6370

6471
# Remove any unwanted .DS_Store files
6572
echo "Removing unnecessary files..."
@@ -91,6 +98,7 @@ echo "Building product package..."
9198
--package "$PKGTMP" \
9299
--identifier "$IDENTIFIER" \
93100
--version "$VERSION" \
101+
${IDENTITY:+"--sign"} ${IDENTITY:+"$IDENTITY"} \
94102
"$PKGOUT"
95103

96104
echo "Product build complete."

0 commit comments

Comments
 (0)