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