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