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
12can_code_sign=false
13
14# This must match the team in the provisioning profile.
15IDENTITY="Zed Industries, Inc."
16APPLE_NOTARIZATION_TEAM="MQ55VZLNZQ"
17
18# Function for displaying help info
19help_info() {
20 echo "
21Usage: ${0##*/} [options] [architecture=host]
22Build the application bundle for macOS.
23
24Options:
25 -d Compile in debug mode
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 i) local_install=true;;
43 h)
44 help_info
45 exit 0
46 ;;
47 esac
48done
49
50shift $((OPTIND-1))
51
52
53# Get release channel
54pushd crates/zed
55channel=$(<RELEASE_CHANNEL)
56export ZED_RELEASE_CHANNEL="${channel}"
57popd
58
59export ZED_BUNDLE=true
60
61cargo_bundle_version=$(cargo -q bundle --help 2>&1 | head -n 1 || echo "")
62if [ "$cargo_bundle_version" != "cargo-bundle v0.6.1-zed" ]; then
63 cargo install cargo-bundle --git https://github.com/zed-industries/cargo-bundle.git --branch zed-deploy
64fi
65
66# Deal with versions of macOS that don't include libstdc++ headers
67export CXXFLAGS="-stdlib=libc++"
68
69version_info=$(rustc --version --verbose)
70host_line=$(echo "$version_info" | grep host)
71target_triple=${host_line#*: }
72if [[ $# -gt 0 && -n "$1" ]]; then
73 target_triple="$1"
74fi
75remote_server_arch=""
76
77if [[ "$target_triple" = "x86_64-apple-darwin" ]]; then
78 remote_server_arch="x86_64"
79elif [[ "$target_triple" = "aarch64-apple-darwin" ]]; then
80 remote_server_arch="aarch64"
81else
82 echo "Unsupported architecture $target_triple"
83 exit 1
84fi
85
86# Generate the licenses first, so they can be baked into the binaries
87script/generate-licenses
88
89rustup target add $target_triple
90
91echo "Compiling zed binaries"
92cargo build ${build_flag} --package zed --package cli --target $target_triple
93# Build remote_server in separate invocation to prevent feature unification from other crates
94# from influencing dynamic libraries required by it.
95cargo build ${build_flag} --package remote_server --target $target_triple
96
97echo "Creating application bundle"
98pushd crates/zed
99cp Cargo.toml Cargo.toml.backup
100sed \
101 -i.backup \
102 "s/package.metadata.bundle-${channel}/package.metadata.bundle/" \
103 Cargo.toml
104
105app_path=$(cargo bundle ${build_flag} --target $target_triple --select-workspace-root | xargs)
106
107mv Cargo.toml.backup Cargo.toml
108popd
109echo "Bundled ${app_path}"
110
111if [[ -n "${MACOS_CERTIFICATE:-}" && -n "${MACOS_CERTIFICATE_PASSWORD:-}" && -n "${APPLE_NOTARIZATION_KEY:-}" && -n "${APPLE_NOTARIZATION_KEY_ID:-}" && -n "${APPLE_NOTARIZATION_ISSUER_ID:-}" ]]; then
112 can_code_sign=true
113
114 echo "Setting up keychain for code signing..."
115 security create-keychain -p "$MACOS_CERTIFICATE_PASSWORD" zed.keychain || echo ""
116 security default-keychain -s zed.keychain
117 security unlock-keychain -p "$MACOS_CERTIFICATE_PASSWORD" zed.keychain
118 echo "$MACOS_CERTIFICATE" | base64 --decode > /tmp/zed-certificate.p12
119 security import /tmp/zed-certificate.p12 -k zed.keychain -P "$MACOS_CERTIFICATE_PASSWORD" -T /usr/bin/codesign
120 rm /tmp/zed-certificate.p12
121 security set-key-partition-list -S apple-tool:,apple:,codesign: -s -k "$MACOS_CERTIFICATE_PASSWORD" zed.keychain
122
123 function cleanup() {
124 echo "Cleaning up keychain"
125 security default-keychain -s login.keychain
126 security delete-keychain zed.keychain
127 }
128
129 trap cleanup EXIT
130fi
131
132GIT_VERSION="v2.43.3"
133GIT_VERSION_SHA="fa29823"
134
135function download_and_unpack() {
136 local url=$1
137 local path_to_unpack=$2
138 local target_path=$3
139
140 temp_dir=$(mktemp -d)
141
142 if ! command -v curl &> /dev/null; then
143 echo "curl is not installed. Please install curl to continue."
144 exit 1
145 fi
146
147 curl --silent --fail --location "$url" | tar -xvz -C "$temp_dir" -f - $path_to_unpack
148
149 mv "$temp_dir/$path_to_unpack" "$target_path"
150
151 rm -rf "$temp_dir"
152}
153
154function download_git() {
155 local architecture=$1
156 local target_binary=$2
157
158 tmp_dir=$(mktemp -d)
159 pushd "$tmp_dir"
160
161 case "$architecture" in
162 aarch64-apple-darwin)
163 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
164 ;;
165 x86_64-apple-darwin)
166 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
167 ;;
168 *)
169 echo "Unsupported architecture: $architecture"
170 exit 1
171 ;;
172 esac
173
174 popd
175
176 mv "${tmp_dir}/git" "${target_binary}"
177 rm -rf "$tmp_dir"
178}
179
180function sign_app_binaries() {
181 rm -rf "${app_path}/Contents/Frameworks"
182 mkdir -p "${app_path}/Contents/Frameworks"
183
184 echo "Downloading git binary"
185 download_git "${target_triple}" "${app_path}/Contents/MacOS/git"
186
187 # Note: The app identifier for our development builds is the same as the app identifier for nightly.
188 cp crates/zed/contents/$channel/embedded.provisionprofile "${app_path}/Contents/"
189
190 if [[ $can_code_sign = true ]]; then
191 echo "Code signing binaries"
192 # sequence of codesign commands modeled after this example: https://developer.apple.com/forums/thread/701514
193 /usr/bin/codesign --deep --force --timestamp --options runtime --sign "$IDENTITY" "${app_path}/Contents/MacOS/cli" -v
194 /usr/bin/codesign --deep --force --timestamp --options runtime --sign "$IDENTITY" "${app_path}/Contents/MacOS/git" -v
195 /usr/bin/codesign --deep --force --timestamp --options runtime --entitlements crates/zed/resources/zed.entitlements --sign "$IDENTITY" "${app_path}/Contents/MacOS/zed" -v
196 /usr/bin/codesign --force --timestamp --options runtime --entitlements crates/zed/resources/zed.entitlements --sign "$IDENTITY" "${app_path}" -v
197 else
198 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"
199 if [[ "$local_only" = false ]]; then
200 echo "To create a self-signed local build use ./scripts/build.sh -ldf"
201 exit 1
202 fi
203
204 echo "====== WARNING ======"
205 echo "This bundle is being signed without all entitlements, some features (e.g. universal links) will not work"
206 echo "====== WARNING ======"
207
208 # NOTE: if you need to test universal links you have a few paths forward:
209 # - create a PR and tag it with the `run-bundling` label, and download the .dmg file from there.
210 # - get a signing key for the MQ55VZLNZQ team from Nathan.
211 # - create your own signing key, and update references to MQ55VZLNZQ to your own team ID
212 # then comment out this line.
213 cat crates/zed/resources/zed.entitlements | sed '/com.apple.developer.associated-domains/,+1d' > "${app_path}/Contents/Resources/zed.entitlements"
214
215 codesign --force --deep --entitlements "${app_path}/Contents/Resources/zed.entitlements" --sign ${MACOS_SIGNING_KEY:- -} "${app_path}" -v
216 fi
217
218 if [[ "$target_dir" = "debug" && "$local_only" = false ]]; then
219 if [ "$open_result" = true ]; then
220 open "$app_path"
221 else
222 echo "Created application bundle:"
223 echo "$app_path"
224 fi
225 exit 0
226 fi
227
228 bundle_name=$(basename "$app_path")
229
230 if [ "$local_only" = true ]; then
231 if [ "$local_install" = true ]; then
232 rm -rf "/Applications/$bundle_name"
233 mv "$app_path" "/Applications/$bundle_name"
234 echo "Installed application bundle: /Applications/$bundle_name"
235 if [ "$open_result" = true ]; then
236 echo "Opening /Applications/$bundle_name"
237 open "/Applications/$bundle_name"
238 fi
239 else
240 if [ "$open_result" = true ]; then
241 echo "Opening $app_path"
242 open "$app_path"
243 fi
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.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}
294cp target/${target_triple}/${target_dir}/zed "${app_path}/Contents/MacOS/zed"
295cp target/${target_triple}/${target_dir}/cli "${app_path}/Contents/MacOS/cli"
296sign_app_binaries
297
298sign_binary "target/$target_triple/release/remote_server"
299gzip -f --stdout --best target/$target_triple/release/remote_server > target/zed-remote-server-macos-$remote_server_arch.gz
300
301function upload_debug_info() {
302 if [[ -n "${SENTRY_AUTH_TOKEN:-}" ]]; then
303 echo "Uploading zed debug symbols to sentry..."
304 # note: this uploads the unstripped binary which is needed because it contains
305 # .eh_frame data for stack unwinding. see https://github.com/getsentry/symbolic/issues/783
306 sentry-cli debug-files upload --include-sources --wait -p zed -o zed-dev \
307 "target/${target_triple}/${target_dir}/zed" \
308 "target/${target_triple}/${target_dir}/remote_server" \
309 "target/${target_triple}/${target_dir}/zed.dwarf"
310 else
311 echo "missing SENTRY_AUTH_TOKEN. skipping sentry upload."
312 fi
313}
314
315if command -v sentry-cli >/dev/null 2>&1; then
316 upload_debug_info
317else
318 echo "sentry-cli not found. skipping sentry upload."
319 echo "install with: 'curl -sL https://sentry.io/get-cli | bash'"
320fi