1#!/usr/bin/env bash
2
3set -euo pipefail
4source script/lib/blob-store.sh
5
6build_flag="--release"
7target_dir="release"
8open_result=false
9local_install=false
10can_code_sign=false
11
12# This must match the team in the provisioning profile.
13IDENTITY="Zed Industries, Inc."
14APPLE_NOTARIZATION_TEAM="MQ55VZLNZQ"
15
16# Function for displaying help info
17help_info() {
18 echo "
19Usage: ${0##*/} [options] [architecture=host]
20Build the application bundle for macOS.
21
22Options:
23 -d Compile in debug mode
24 -o Open dir with the resulting DMG or launch the app itself in local mode.
25 -i Install the resulting DMG into /Applications.
26 -h Display this help and exit.
27 "
28}
29
30while getopts 'dloih' flag
31do
32 case "${flag}" in
33 o) open_result=true;;
34 d)
35 export CARGO_INCREMENTAL=true
36 export CARGO_BUNDLE_SKIP_BUILD=true
37 build_flag="";
38 target_dir="debug"
39 ;;
40 i) local_install=true;;
41 h)
42 help_info
43 exit 0
44 ;;
45 esac
46done
47
48shift $((OPTIND-1))
49
50
51# Get release channel
52pushd crates/zed
53channel=$(<RELEASE_CHANNEL)
54export ZED_RELEASE_CHANNEL="${channel}"
55popd
56
57export ZED_BUNDLE=true
58
59cargo_bundle_version=$(cargo -q bundle --help 2>&1 | head -n 1 || echo "")
60if [ "$cargo_bundle_version" != "cargo-bundle v0.6.1-zed" ]; then
61 cargo install cargo-bundle --git https://github.com/zed-industries/cargo-bundle.git --branch zed-deploy
62fi
63
64# Deal with versions of macOS that don't include libstdc++ headers
65export CXXFLAGS="-stdlib=libc++"
66
67version_info=$(rustc --version --verbose)
68host_line=$(echo "$version_info" | grep host)
69target_triple=${host_line#*: }
70if [[ $# -gt 0 && -n "$1" ]]; then
71 target_triple="$1"
72fi
73arch_suffix=""
74
75if [[ "$target_triple" = "x86_64-apple-darwin" ]]; then
76 arch_suffix="x86_64"
77elif [[ "$target_triple" = "aarch64-apple-darwin" ]]; then
78 arch_suffix="aarch64"
79else
80 echo "Unsupported architecture $target_triple"
81 exit 1
82fi
83
84# Generate the licenses first, so they can be baked into the binaries
85script/generate-licenses
86
87rustup target add $target_triple
88
89echo "Compiling zed binaries"
90cargo build ${build_flag} --package zed --package cli --target $target_triple
91# Build remote_server in separate invocation to prevent feature unification from other crates
92# from influencing dynamic libraries required by it.
93cargo build ${build_flag} --package remote_server --target $target_triple
94
95echo "Creating application bundle"
96pushd crates/zed
97cp Cargo.toml Cargo.toml.backup
98sed \
99 -i.backup \
100 "s/package.metadata.bundle-${channel}/package.metadata.bundle/" \
101 Cargo.toml
102
103app_path=$(cargo bundle ${build_flag} --target $target_triple --select-workspace-root | xargs)
104
105mv Cargo.toml.backup Cargo.toml
106popd
107echo "Bundled ${app_path}"
108
109# DocumentTypes.plist references CFBundleTypeIconFile "Document", so the bundle must contain Document.icns.
110# We use the app icon as a placeholder document icon for now.
111document_icon_source="crates/zed/resources/Document.icns"
112document_icon_target="${app_path}/Contents/Resources/Document.icns"
113if [[ -f "${document_icon_source}" ]]; then
114 mkdir -p "$(dirname "${document_icon_target}")"
115 cp "${document_icon_source}" "${document_icon_target}"
116else
117 echo "cargo::warning=Missing ${document_icon_source}; macOS document icons may not appear in Finder."
118fi
119
120if [[ -n "${MACOS_CERTIFICATE:-}" && -n "${MACOS_CERTIFICATE_PASSWORD:-}" && -n "${APPLE_NOTARIZATION_KEY:-}" && -n "${APPLE_NOTARIZATION_KEY_ID:-}" && -n "${APPLE_NOTARIZATION_ISSUER_ID:-}" ]]; then
121 can_code_sign=true
122
123 echo "Setting up keychain for code signing..."
124 security create-keychain -p "$MACOS_CERTIFICATE_PASSWORD" zed.keychain || echo ""
125 security default-keychain -s zed.keychain
126 security unlock-keychain -p "$MACOS_CERTIFICATE_PASSWORD" zed.keychain
127 # Calling set-keychain-settings without `-t` disables the auto-lock timeout
128 security set-keychain-settings zed.keychain
129 echo "$MACOS_CERTIFICATE" | base64 --decode > /tmp/zed-certificate.p12
130 security import /tmp/zed-certificate.p12 -k zed.keychain -P "$MACOS_CERTIFICATE_PASSWORD" -T /usr/bin/codesign
131 rm /tmp/zed-certificate.p12
132 security set-key-partition-list -S apple-tool:,apple:,codesign: -s -k "$MACOS_CERTIFICATE_PASSWORD" zed.keychain
133
134 function cleanup() {
135 echo "Cleaning up keychain"
136 security default-keychain -s login.keychain
137 security delete-keychain zed.keychain
138 }
139
140 trap cleanup EXIT
141fi
142
143GIT_VERSION="v2.43.3"
144GIT_VERSION_SHA="fa29823"
145
146function download_and_unpack() {
147 local url=$1
148 local path_to_unpack=$2
149 local target_path=$3
150
151 temp_dir=$(mktemp -d)
152
153 if ! command -v curl &> /dev/null; then
154 echo "curl is not installed. Please install curl to continue."
155 exit 1
156 fi
157
158 curl --silent --fail --location "$url" | tar -xvz -C "$temp_dir" -f - $path_to_unpack
159
160 mv "$temp_dir/$path_to_unpack" "$target_path"
161
162 rm -rf "$temp_dir"
163}
164
165function download_git() {
166 local architecture=$1
167 local target_binary=$2
168
169 tmp_dir=$(mktemp -d)
170 pushd "$tmp_dir"
171
172 case "$architecture" in
173 aarch64-apple-darwin)
174 download_and_unpack "https://github.com/desktop/dugite-native/releases/download/${GIT_VERSION}/dugite-native-${GIT_VERSION}-${GIT_VERSION_SHA}-macOS-arm64.tar.gz" bin/git ./git
175 ;;
176 x86_64-apple-darwin)
177 download_and_unpack "https://github.com/desktop/dugite-native/releases/download/${GIT_VERSION}/dugite-native-${GIT_VERSION}-${GIT_VERSION_SHA}-macOS-x64.tar.gz" bin/git ./git
178 ;;
179 *)
180 echo "Unsupported architecture: $architecture"
181 exit 1
182 ;;
183 esac
184
185 popd
186
187 mv "${tmp_dir}/git" "${target_binary}"
188 rm -rf "$tmp_dir"
189}
190
191function sign_app_binaries() {
192 rm -rf "${app_path}/Contents/Frameworks"
193 mkdir -p "${app_path}/Contents/Frameworks"
194
195 echo "Downloading git binary"
196 download_git "${target_triple}" "${app_path}/Contents/MacOS/git"
197
198 # Note: The app identifier for our development builds is the same as the app identifier for nightly.
199 cp crates/zed/contents/$channel/embedded.provisionprofile "${app_path}/Contents/"
200
201 if [[ $can_code_sign = true ]]; then
202 echo "Code signing binaries"
203 # sequence of codesign commands modeled after this example: https://developer.apple.com/forums/thread/701514
204 /usr/bin/codesign --deep --force --timestamp --options runtime --sign "$IDENTITY" "${app_path}/Contents/MacOS/cli" -v
205 /usr/bin/codesign --deep --force --timestamp --options runtime --sign "$IDENTITY" "${app_path}/Contents/MacOS/git" -v
206 /usr/bin/codesign --deep --force --timestamp --options runtime --entitlements crates/zed/resources/zed.entitlements --sign "$IDENTITY" "${app_path}/Contents/MacOS/zed" -v
207 /usr/bin/codesign --force --timestamp --options runtime --entitlements crates/zed/resources/zed.entitlements --sign "$IDENTITY" "${app_path}" -v
208 else
209 echo "One or more of the following variables are missing: MACOS_CERTIFICATE, MACOS_CERTIFICATE_PASSWORD, APPLE_NOTARIZATION_KEY, APPLE_NOTARIZATION_KEY_ID, APPLE_NOTARIZATION_ISSUER_ID"
210
211 echo "====== WARNING ======"
212 echo "This bundle is being signed without all entitlements, some features (e.g. universal links) will not work"
213 echo "====== WARNING ======"
214
215 # NOTE: if you need to test universal links you have a few paths forward:
216 # - create a PR and tag it with the `run-bundling` label, and download the .dmg file from there.
217 # - get a signing key for the MQ55VZLNZQ team from Nathan.
218 # - create your own signing key, and update references to MQ55VZLNZQ to your own team ID
219 # then comment out this line.
220 cat crates/zed/resources/zed.entitlements | sed '/com.apple.developer.associated-domains/,+1d' > "${app_path}/Contents/Resources/zed.entitlements"
221
222 codesign --force --deep --entitlements "${app_path}/Contents/Resources/zed.entitlements" --sign ${MACOS_SIGNING_KEY:- -} "${app_path}" -v
223 fi
224
225 bundle_name=$(basename "$app_path")
226
227 if [ "$local_install" = true ]; then
228 rm -rf "/Applications/$bundle_name"
229 mv "$app_path" "/Applications/$bundle_name"
230 echo "Installed application bundle: /Applications/$bundle_name"
231 if [ "$open_result" = true ]; then
232 echo "Opening /Applications/$bundle_name"
233 open "/Applications/$bundle_name"
234 fi
235 elif [ "$open_result" = true ]; then
236 open "$app_path"
237 fi
238
239 if [[ "$target_dir" = "debug" ]]; then
240 echo "Debug build detected - skipping DMG creation and signing"
241 if [ "$local_install" = false ]; then
242 echo "Created application bundle:"
243 echo "$app_path"
244 fi
245 else
246 dmg_target_directory="target/${target_triple}/${target_dir}"
247 dmg_source_directory="${dmg_target_directory}/dmg"
248 dmg_file_path="${dmg_target_directory}/Zed-${arch_suffix}.dmg"
249 xcode_bin_dir_path="$(xcode-select -p)/usr/bin"
250
251 rm -rf ${dmg_source_directory}
252 mkdir -p ${dmg_source_directory}
253 mv "${app_path}" "${dmg_source_directory}"
254 notarization_key_file=$(mktemp)
255
256 echo "Adding symlink to /Applications to ${dmg_source_directory}"
257 ln -s /Applications ${dmg_source_directory}
258
259 echo "Creating final DMG at ${dmg_file_path} using ${dmg_source_directory}"
260 hdiutil create -volname Zed -srcfolder "${dmg_source_directory}" -ov -format UDZO "${dmg_file_path}"
261
262 # If someone runs this bundle script locally, a symlink will be placed in `dmg_source_directory`.
263 # This symlink causes CPU issues with Zed if the Zed codebase is the project being worked on, so we simply remove it for now.
264 echo "Removing symlink to /Applications from ${dmg_source_directory}"
265 rm ${dmg_source_directory}/Applications
266
267 echo "Adding license agreement to DMG"
268 npm install --global dmg-license minimist
269 dmg-license script/terms/terms.json "${dmg_file_path}"
270
271 if [[ $can_code_sign = true ]]; then
272 echo "Notarizing DMG with Apple"
273 /usr/bin/codesign --deep --force --timestamp --options runtime --sign "$IDENTITY" "$(pwd)/${dmg_file_path}" -v
274 echo "$APPLE_NOTARIZATION_KEY" > "$notarization_key_file"
275 "${xcode_bin_dir_path}/notarytool" submit --wait --key "$notarization_key_file" --key-id "$APPLE_NOTARIZATION_KEY_ID" --issuer "$APPLE_NOTARIZATION_ISSUER_ID" "${dmg_file_path}"
276 rm "$notarization_key_file"
277 "${xcode_bin_dir_path}/stapler" staple "${dmg_file_path}"
278 fi
279
280 if [ "$open_result" = true ]; then
281 open $dmg_target_directory
282 fi
283 fi
284}
285
286function sign_binary() {
287 local binary_path=$1
288
289 if [[ $can_code_sign = true ]]; then
290 echo "Code signing executable $binary_path"
291 /usr/bin/codesign --deep --force --timestamp --options runtime --entitlements crates/zed/resources/zed.entitlements --sign "$IDENTITY" "${binary_path}" -v
292 fi
293}
294
295function upload_debug_symbols() {
296 if [ "$local_install" = true ]; then
297 echo "local install; skipping sentry upload."
298 elif [[ -n "${SENTRY_AUTH_TOKEN:-}" ]]; then
299 echo "Uploading zed debug symbols to sentry..."
300 exe_path="target/${target_triple}/release/Zed"
301 if ! dsymutil --flat "target/${target_triple}/${target_dir}/zed" 2> target/dsymutil.log; then
302 echo "dsymutil failed"
303 cat target/dsymutil.log
304 exit 1
305 fi
306 if ! dsymutil --flat "target/${target_triple}/${target_dir}/remote_server" 2> target/dsymutil.log; then
307 echo "dsymutil failed"
308 cat target/dsymutil.log
309 exit 1
310 fi
311 # note: this uploads the unstripped binary which is needed because it contains
312 # .eh_frame data for stack unwinding. see https://github.com/getsentry/symbolic/issues/783
313 sentry-cli debug-files upload --include-sources --wait -p zed -o zed-dev \
314 # Try uploading up to 3 times
315 for attempt in 1 2 3; do
316 echo "Sentry upload attempt $attempt..."
317 if sentry-cli debug-files upload --include-sources --wait -p zed -o zed-dev \
318 "target/${target_triple}/${target_dir}/zed.dwarf" \
319 "target/${target_triple}/${target_dir}/remote_server.dwarf"; then
320 break
321 else
322 echo "Sentry upload failed on attempt $attempt"
323 if [ $attempt -eq 3 ]; then
324 echo "All sentry upload attempts failed"
325 exit 1
326 fi
327 fi
328 done
329 else
330 echo "missing SENTRY_AUTH_TOKEN. skipping sentry upload."
331 fi
332}
333
334upload_debug_symbols
335
336cp target/${target_triple}/${target_dir}/zed "${app_path}/Contents/MacOS/zed"
337cp target/${target_triple}/${target_dir}/cli "${app_path}/Contents/MacOS/cli"
338sign_app_binaries
339
340sign_binary "target/$target_triple/release/remote_server"
341gzip -f --stdout --best target/$target_triple/release/remote_server > target/zed-remote-server-macos-$arch_suffix.gz