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