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