-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.sh
More file actions
executable file
·176 lines (148 loc) · 5.59 KB
/
Copy pathbuild.sh
File metadata and controls
executable file
·176 lines (148 loc) · 5.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#!/usr/bin/env bash
set -euo pipefail
ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
cd "$ROOT_DIR"
APP_NAME="MacTempMenuBar"
BUILD_DIR="$ROOT_DIR/build"
APP_DIR="$BUILD_DIR/${APP_NAME}.app"
DIST_DIR="$ROOT_DIR/dist"
SIGN_IDENTITY="${SIGN_IDENTITY:-}"
function require_command() {
local cmd="$1"
if ! command -v "$cmd" >/dev/null 2>&1; then
echo "Required command not found: $cmd" >&2
exit 1
fi
}
require_command xcrun
require_command lipo
require_command curl
require_command ditto
SDKROOT="$(xcrun --sdk macosx --show-sdk-path)"
# Sparkle (auto update) framework.
SPARKLE_ROOT="$("$ROOT_DIR/Tools/fetch_sparkle.sh")"
rm -rf "$BUILD_DIR"
mkdir -p "$BUILD_DIR"
# Generate app icon (.icns).
ICON_PNG="$BUILD_DIR/AppIcon.png"
ICONSET_DIR="$BUILD_DIR/AppIcon.iconset"
ICON_ICNS="$BUILD_DIR/AppIcon.icns"
swift "$ROOT_DIR/Tools/generate_icon.swift" "$ICON_PNG" >/dev/null
rm -rf "$ICONSET_DIR"
mkdir -p "$ICONSET_DIR"
function _sips_resize() {
local w="$1"
local h="$2"
local out="$3"
sips -z "$h" "$w" "$ICON_PNG" --out "$out" >/dev/null
}
_sips_resize 16 16 "$ICONSET_DIR/icon_16x16.png"
_sips_resize 32 32 "$ICONSET_DIR/icon_16x16@2x.png"
_sips_resize 32 32 "$ICONSET_DIR/icon_32x32.png"
_sips_resize 64 64 "$ICONSET_DIR/icon_32x32@2x.png"
_sips_resize 128 128 "$ICONSET_DIR/icon_128x128.png"
_sips_resize 256 256 "$ICONSET_DIR/icon_128x128@2x.png"
_sips_resize 256 256 "$ICONSET_DIR/icon_256x256.png"
_sips_resize 512 512 "$ICONSET_DIR/icon_256x256@2x.png"
_sips_resize 512 512 "$ICONSET_DIR/icon_512x512.png"
cp "$ICON_PNG" "$ICONSET_DIR/icon_512x512@2x.png"
iconutil -c icns "$ICONSET_DIR" -o "$ICON_ICNS"
# Compile a universal binary (arm64 + x86_64) so releases work on both Apple Silicon and Intel Macs.
TARGET_TRIPLE_ARM64="arm64-apple-macos13.0"
TARGET_TRIPLE_X86_64="x86_64-apple-macos13.0"
SMC_OBJ_ARM64="$BUILD_DIR/SMCBridge-arm64.o"
SMC_OBJ_X86_64="$BUILD_DIR/SMCBridge-x86_64.o"
BIN_ARM64="$BUILD_DIR/${APP_NAME}-arm64"
BIN_X86_64="$BUILD_DIR/${APP_NAME}-x86_64"
# Compile the SMC bridge (per-arch).
clang -c -O2 -Wall -Wextra -isysroot "$SDKROOT" -target "$TARGET_TRIPLE_ARM64" "$ROOT_DIR/App/SMCBridge.c" -o "$SMC_OBJ_ARM64"
clang -c -O2 -Wall -Wextra -isysroot "$SDKROOT" -target "$TARGET_TRIPLE_X86_64" "$ROOT_DIR/App/SMCBridge.c" -o "$SMC_OBJ_X86_64"
# Compile and link the Swift menu bar app (per-arch).
swiftc \
-parse-as-library \
-O \
-sdk "$SDKROOT" \
-target "$TARGET_TRIPLE_ARM64" \
-F "$SPARKLE_ROOT" \
-import-objc-header "$ROOT_DIR/App/BridgingHeader.h" \
"$ROOT_DIR/App/AppMain.swift" \
"$SMC_OBJ_ARM64" \
-framework AppKit \
-framework Foundation \
-framework IOKit \
-framework CoreFoundation \
-framework Sparkle \
-Xlinker -rpath -Xlinker @loader_path/../Frameworks \
-o "$BIN_ARM64"
swiftc \
-parse-as-library \
-O \
-sdk "$SDKROOT" \
-target "$TARGET_TRIPLE_X86_64" \
-F "$SPARKLE_ROOT" \
-import-objc-header "$ROOT_DIR/App/BridgingHeader.h" \
"$ROOT_DIR/App/AppMain.swift" \
"$SMC_OBJ_X86_64" \
-framework AppKit \
-framework Foundation \
-framework IOKit \
-framework CoreFoundation \
-framework Sparkle \
-Xlinker -rpath -Xlinker @loader_path/../Frameworks \
-o "$BIN_X86_64"
# Combine into a universal binary.
lipo -create -output "$BUILD_DIR/${APP_NAME}" "$BIN_ARM64" "$BIN_X86_64"
# Build the .app bundle.
mkdir -p "$APP_DIR/Contents/MacOS"
mkdir -p "$APP_DIR/Contents/Resources"
mkdir -p "$APP_DIR/Contents/Frameworks"
cp "$ROOT_DIR/App/Info.plist" "$APP_DIR/Contents/Info.plist"
cp "$BUILD_DIR/${APP_NAME}" "$APP_DIR/Contents/MacOS/${APP_NAME}"
cp "$ICON_ICNS" "$APP_DIR/Contents/Resources/AppIcon.icns"
# Embed Sparkle.framework (includes its helper app + XPC services).
ditto "$SPARKLE_ROOT/Sparkle.framework" "$APP_DIR/Contents/Frameworks/Sparkle.framework"
# Sign the app bundle.
# - If SIGN_IDENTITY is set, use Developer ID + Hardened Runtime (required for notarization).
# - Otherwise, fall back to ad-hoc sign for local usage.
if command -v codesign >/dev/null 2>&1; then
FRAMEWORK_PATH="$APP_DIR/Contents/Frameworks/Sparkle.framework"
if [[ -n "$SIGN_IDENTITY" ]]; then
# Sign nested code inside the framework first.
if [[ -d "$FRAMEWORK_PATH/Versions/B/XPCServices" ]]; then
for xpc in "$FRAMEWORK_PATH/Versions/B/XPCServices/"*.xpc; do
if [[ -d "$xpc" ]]; then
codesign --force --sign "$SIGN_IDENTITY" --timestamp --options runtime "$xpc"
fi
done
fi
if [[ -d "$FRAMEWORK_PATH/Versions/B/Updater.app" ]]; then
codesign --force --sign "$SIGN_IDENTITY" --timestamp --options runtime "$FRAMEWORK_PATH/Versions/B/Updater.app"
fi
codesign --force --sign "$SIGN_IDENTITY" --timestamp --options runtime "$FRAMEWORK_PATH"
codesign \
--force \
--sign "$SIGN_IDENTITY" \
--timestamp \
--options runtime \
"$APP_DIR"
codesign --verify --deep --strict --verbose=2 "$APP_DIR"
else
# Ad-hoc sign nested framework code to avoid "not signed at all" issues in local builds.
if [[ -d "$FRAMEWORK_PATH/Versions/B/XPCServices" ]]; then
for xpc in "$FRAMEWORK_PATH/Versions/B/XPCServices/"*.xpc; do
if [[ -d "$xpc" ]]; then
codesign --force --sign - "$xpc" >/dev/null 2>&1 || true
fi
done
fi
if [[ -d "$FRAMEWORK_PATH/Versions/B/Updater.app" ]]; then
codesign --force --sign - "$FRAMEWORK_PATH/Versions/B/Updater.app" >/dev/null 2>&1 || true
fi
codesign --force --sign - "$FRAMEWORK_PATH" >/dev/null 2>&1 || true
codesign --force --sign - "$APP_DIR" >/dev/null 2>&1 || true
fi
fi
rm -rf "$DIST_DIR"
mkdir -p "$DIST_DIR"
cp -R "$APP_DIR" "$DIST_DIR/"
echo "Built: $DIST_DIR/${APP_NAME}.app"