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