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_NOTORIZATION_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
66export ZED_BUNDLE=true
67export MACOSX_DEPLOYMENT_TARGET=10.15.7
68
69cargo_bundle_version=$(cargo -q bundle --help 2>&1 | head -n 1 || echo "")
70if [ "$cargo_bundle_version" != "cargo-bundle v0.6.0-zed" ]; then
71 cargo install cargo-bundle --git https://github.com/zed-industries/cargo-bundle.git --branch zed-deploy
72fi
73
74# Deal with versions of macOS that don't include libstdc++ headers
75export CXXFLAGS="-stdlib=libc++"
76
77version_info=$(rustc --version --verbose)
78host_line=$(echo "$version_info" | grep host)
79local_target_triple=${host_line#*: }
80
81if [ "$local_arch" = true ]; then
82 echo "Building for local target only."
83 cargo build ${build_flag} --package zed --package cli --package remote_server
84else
85 echo "Compiling zed binaries"
86 cargo build ${build_flag} --package zed --package cli --package remote_server --target aarch64-apple-darwin --target x86_64-apple-darwin
87fi
88
89echo "Creating application bundle"
90pushd crates/zed
91channel=$(<RELEASE_CHANNEL)
92popd
93
94pushd crates/zed
95cp Cargo.toml Cargo.toml.backup
96sed \
97 -i .backup \
98 "s/package.metadata.bundle-${channel}/package.metadata.bundle/" \
99 Cargo.toml
100
101if [ "$local_arch" = true ]; then
102 app_path=$(cargo bundle ${build_flag} --select-workspace-root | xargs)
103else
104 app_path_x64=$(cargo bundle ${build_flag} --target x86_64-apple-darwin --select-workspace-root | xargs)
105 app_path_aarch64=$(cargo bundle ${build_flag} --target aarch64-apple-darwin --select-workspace-root | xargs)
106 app_path=$app_path_x64
107fi
108
109mv Cargo.toml.backup Cargo.toml
110popd
111echo "Bundled ${app_path}"
112
113if [[ -n "${MACOS_CERTIFICATE:-}" && -n "${MACOS_CERTIFICATE_PASSWORD:-}" && -n "${APPLE_NOTARIZATION_USERNAME:-}" && -n "${APPLE_NOTARIZATION_PASSWORD:-}" ]]; then
114 can_code_sign=true
115
116 echo "Setting up keychain for code signing..."
117 security create-keychain -p "$MACOS_CERTIFICATE_PASSWORD" zed.keychain || echo ""
118 security default-keychain -s zed.keychain
119 security unlock-keychain -p "$MACOS_CERTIFICATE_PASSWORD" zed.keychain
120 echo "$MACOS_CERTIFICATE" | base64 --decode > /tmp/zed-certificate.p12
121 security import /tmp/zed-certificate.p12 -k zed.keychain -P "$MACOS_CERTIFICATE_PASSWORD" -T /usr/bin/codesign
122 rm /tmp/zed-certificate.p12
123 security set-key-partition-list -S apple-tool:,apple:,codesign: -s -k "$MACOS_CERTIFICATE_PASSWORD" zed.keychain
124
125 function cleanup() {
126 echo "Cleaning up keychain"
127 security default-keychain -s login.keychain
128 security delete-keychain zed.keychain
129 }
130
131 trap cleanup EXIT
132fi
133
134GIT_VERSION="v2.43.3"
135GIT_VERSION_SHA="fa29823"
136
137function download_and_unpack() {
138 local url=$1
139 local path_to_unpack=$2
140 local target_path=$3
141
142 temp_dir=$(mktemp -d)
143
144 if ! command -v curl &> /dev/null; then
145 echo "curl is not installed. Please install curl to continue."
146 exit 1
147 fi
148
149 curl --silent --fail --location "$url" | tar -xvz -C "$temp_dir" -f - $path_to_unpack
150
151 mv "$temp_dir/$path_to_unpack" "$target_path"
152
153 rm -rf "$temp_dir"
154}
155
156function download_git() {
157 local architecture=$1
158 local target_binary=$2
159
160 tmp_dir=$(mktemp -d)
161 pushd "$tmp_dir"
162
163 case "$architecture" in
164 aarch64-apple-darwin)
165 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
166 ;;
167 x86_64-apple-darwin)
168 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
169 ;;
170 universal)
171 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_arm64
172 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_x64
173 lipo -create ./git_arm64 ./git_x64 -output ./git
174 ;;
175 *)
176 echo "Unsupported architecture: $architecture"
177 exit 1
178 ;;
179 esac
180
181 popd
182
183 mv "${tmp_dir}/git" "${target_binary}"
184 rm -rf "$tmp_dir"
185}
186
187function prepare_binaries() {
188 local architecture=$1
189 local app_path=$2
190
191 echo "Unpacking dSYMs for $architecture"
192 if ! dsymutil --flat target/${architecture}/${target_dir}/Zed 2> target/dsymutil.log; then
193 echo "dsymutil failed"
194 cat target/dsymutil.log
195 exit 1
196 fi
197 version="$(cargo metadata --no-deps --manifest-path crates/zed/Cargo.toml --offline --format-version=1 | jq -r '.packages | map(select(.name == "zed"))[0].version')"
198 if [ "$channel" == "nightly" ]; then
199 version="$version-$(git rev-parse --short HEAD)"
200 fi
201
202 echo "Removing existing gzipped dSYMs for $architecture"
203 rm -f target/${architecture}/${target_dir}/Zed.dwarf.gz
204
205 echo "Gzipping dSYMs for $architecture"
206 gzip target/${architecture}/${target_dir}/Zed.dwarf
207
208 echo "Uploading dSYMs for $architecture"
209 upload_to_blob_store_public \
210 "zed-debug-symbols" \
211 target/${architecture}/${target_dir}/Zed.dwarf.gz \
212 "$channel/Zed-$version-${architecture}.dwarf.gz"
213
214 cp target/${architecture}/${target_dir}/zed "${app_path}/Contents/MacOS/zed"
215 cp target/${architecture}/${target_dir}/cli "${app_path}/Contents/MacOS/cli"
216}
217
218function sign_app_binaries() {
219 local app_path=$1
220 local architecture=$2
221 local architecture_dir=$3
222 echo "Copying WebRTC.framework into the frameworks folder"
223 mkdir "${app_path}/Contents/Frameworks"
224 if [ "$local_arch" = false ]; then
225 cp -R target/${local_target_triple}/${target_dir}/WebRTC.framework "${app_path}/Contents/Frameworks/"
226 else
227 cp -R target/${target_dir}/WebRTC.framework "${app_path}/Contents/Frameworks/"
228 cp -R target/${target_dir}/cli "${app_path}/Contents/MacOS/"
229 fi
230
231 echo "Downloading git binary"
232 download_git "${architecture}" "${app_path}/Contents/MacOS/git"
233
234 # Note: The app identifier for our development builds is the same as the app identifier for nightly.
235 cp crates/zed/contents/$channel/embedded.provisionprofile "${app_path}/Contents/"
236
237 if [[ $can_code_sign = true ]]; then
238 echo "Code signing binaries"
239 # sequence of codesign commands modeled after this example: https://developer.apple.com/forums/thread/701514
240 /usr/bin/codesign --deep --force --timestamp --sign "$IDENTITY" "${app_path}/Contents/Frameworks/WebRTC.framework" -v
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_USERNAME, APPLE_NOTARIZATION_PASSWORD"
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
311 if [[ $can_code_sign = true ]]; then
312 echo "Creating temporary DMG at ${dmg_file_path} using ${dmg_source_directory} to notarize app bundle"
313 hdiutil create -volname Zed -srcfolder "${dmg_source_directory}" -ov -format UDZO "${dmg_file_path}"
314
315 echo "Code-signing DMG"
316 /usr/bin/codesign --deep --force --timestamp --options runtime --sign "$IDENTITY" "$(pwd)/${dmg_file_path}" -v
317
318 echo "Notarizing DMG with Apple"
319 "${xcode_bin_dir_path}/notarytool" submit --wait --apple-id "$APPLE_NOTARIZATION_USERNAME" --password "$APPLE_NOTARIZATION_PASSWORD" --team-id "$APPLE_NOTORIZATION_TEAM" "${dmg_file_path}"
320
321 echo "Removing temporary DMG (used only for notarization)"
322 rm "${dmg_file_path}"
323
324 echo "Stapling notarization ticket to ${dmg_source_directory}/${bundle_name}"
325 "${xcode_bin_dir_path}/stapler" staple "${dmg_source_directory}/${bundle_name}"
326 fi
327
328 echo "Adding symlink to /Applications to ${dmg_source_directory}"
329 ln -s /Applications ${dmg_source_directory}
330
331 echo "Creating final DMG at ${dmg_file_path} using ${dmg_source_directory}"
332 hdiutil create -volname Zed -srcfolder "${dmg_source_directory}" -ov -format UDZO "${dmg_file_path}"
333
334 # If someone runs this bundle script locally, a symlink will be placed in `dmg_source_directory`.
335 # This symlink causes CPU issues with Zed if the Zed codebase is the project being worked on, so we simply remove it for now.
336 echo "Removing symlink to /Applications from ${dmg_source_directory}"
337 rm ${dmg_source_directory}/Applications
338
339 echo "Adding license agreement to DMG"
340 npm install --global dmg-license minimist
341 dmg-license script/terms/terms.json "${dmg_file_path}"
342
343 if [[ $can_code_sign = true ]]; then
344 echo "Notarizing DMG with Apple"
345 /usr/bin/codesign --deep --force --timestamp --options runtime --sign "$IDENTITY" "$(pwd)/${dmg_file_path}" -v
346 "${xcode_bin_dir_path}/notarytool" submit --wait --apple-id "$APPLE_NOTARIZATION_USERNAME" --password "$APPLE_NOTARIZATION_PASSWORD" --team-id "$APPLE_NOTORIZATION_TEAM" "${dmg_file_path}"
347 "${xcode_bin_dir_path}/stapler" staple "${dmg_file_path}"
348 fi
349
350 if [ "$open_result" = true ]; then
351 open $dmg_target_directory
352 fi
353 fi
354}
355
356function sign_binary() {
357 local binary_path=$1
358
359 if [[ $can_code_sign = true ]]; then
360 echo "Code signing executable $binary_path"
361 /usr/bin/codesign --deep --force --timestamp --options runtime --sign "$IDENTITY" "${binary_path}" -v
362 fi
363}
364
365if [ "$local_arch" = true ]; then
366 sign_app_binaries "$app_path" "$local_target_triple" "$local_target_triple"
367
368 sign_binary "target/release/remote_server"
369else
370 # Create universal binary
371 prepare_binaries "aarch64-apple-darwin" "$app_path_aarch64"
372 prepare_binaries "x86_64-apple-darwin" "$app_path_x64"
373
374 cp -R "$app_path_x64" target/release/
375 app_path=target/release/$(basename "$app_path_x64")
376 lipo \
377 -create \
378 target/{x86_64-apple-darwin,aarch64-apple-darwin}/${target_dir}/zed \
379 -output \
380 "${app_path}/Contents/MacOS/zed"
381 lipo \
382 -create \
383 target/{x86_64-apple-darwin,aarch64-apple-darwin}/${target_dir}/cli \
384 -output \
385 "${app_path}/Contents/MacOS/cli"
386
387 sign_app_binaries "$app_path" "universal" "."
388 sign_app_binaries "$app_path_x64" "x86_64-apple-darwin" "x86_64-apple-darwin"
389 sign_app_binaries "$app_path_aarch64" "aarch64-apple-darwin" "aarch64-apple-darwin"
390
391 sign_binary "target/x86_64-apple-darwin/release/remote_server"
392 sign_binary "target/aarch64-apple-darwin/release/remote_server"
393 gzip --stdout --best target/x86_64-apple-darwin/release/remote_server > target/zed-remote-server-macos-x86_64.gz
394 gzip --stdout --best target/aarch64-apple-darwin/release/remote_server > target/zed-remote-server-macos-aarch64.gz
395fi