1#!/bin/bash
2
3set -e
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.
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 [ "$1" ]; then
85 bundle_name=$1
86fi
87
88export ZED_BUNDLE=true
89export MACOSX_DEPLOYMENT_TARGET=10.15.7
90
91cargo_bundle_version=$(cargo -q bundle --help 2>&1 | head -n 1 || echo "")
92if [ "$cargo_bundle_version" != "cargo-bundle v0.6.0-zed" ]; then
93 cargo install cargo-bundle --git https://github.com/zed-industries/cargo-bundle.git --branch zed-deploy
94fi
95
96rustup target add wasm32-wasi
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}
108 cargo build ${build_flag} --package cli
109else
110 echo "Compiling zed binaries"
111 cargo build ${build_flag} --package ${zed_crate} --package cli --target aarch64-apple-darwin --target x86_64-apple-darwin
112fi
113
114echo "Creating application bundle"
115pushd crates/zed
116channel=$(<RELEASE_CHANNEL)
117popd
118
119pushd crates/${zed_crate}
120cp Cargo.toml Cargo.toml.backup
121sed \
122 -i .backup \
123 "s/package.metadata.bundle-${channel}/package.metadata.bundle/" \
124 Cargo.toml
125
126if [ "$local_arch" = true ]; then
127 app_path=$(cargo bundle ${build_flag} --select-workspace-root | xargs)
128else
129 app_path=$(cargo bundle ${build_flag} --target x86_64-apple-darwin --select-workspace-root | xargs)
130fi
131
132mv Cargo.toml.backup Cargo.toml
133popd
134echo "Bundled ${app_path}"
135
136if [ "$local_arch" = false ]; then
137 echo "Uploading dSYMs"
138 dsymutil --flat target/aarch64-apple-darwin/release/Zed
139 dsymutil --flat target/x86_64-apple-darwin/release/Zed
140 version="$(cargo metadata --no-deps --manifest-path crates/zed/Cargo.toml --offline --format-version=1 | jq -r '.packages | map(select(.name == "zed"))[0].version')"
141 if [ "$channel" == "nightly" ]; then
142 version="$version-$(git rev-parse --short HEAD)"
143 fi
144
145 echo "Removing existing gzipped dSYMs"
146 rm -f target/aarch64-apple-darwin/release/Zed.dwarf.gz
147 rm -f target/x86_64-apple-darwin/release/Zed.dwarf.gz
148
149 echo "Gzipping dSYMs"
150 gzip target/aarch64-apple-darwin/release/Zed.dwarf
151 gzip target/x86_64-apple-darwin/release/Zed.dwarf
152
153 echo "Uploading dSYMs"
154 uploadDsym target/aarch64-apple-darwin/release/Zed.dwarf.gz "$channel/Zed-$version-aarch64-apple-darwin.dwarf.gz"
155 uploadDsym target/x86_64-apple-darwin/release/Zed.dwarf.gz "$channel/Zed-$version-x86_64-apple-darwin.dwarf.gz"
156
157 echo "Creating fat binaries"
158 lipo \
159 -create \
160 target/{x86_64-apple-darwin,aarch64-apple-darwin}/${target_dir}/${binary_name} \
161 -output \
162 "${app_path}/Contents/MacOS/${zed_crate}"
163 lipo \
164 -create \
165 target/{x86_64-apple-darwin,aarch64-apple-darwin}/${target_dir}/cli \
166 -output \
167 "${app_path}/Contents/MacOS/cli"
168fi
169
170echo "Copying WebRTC.framework into the frameworks folder"
171mkdir "${app_path}/Contents/Frameworks"
172if [ "$local_arch" = false ]; then
173 cp -R target/${local_target_triple}/${target_dir}/WebRTC.framework "${app_path}/Contents/Frameworks/"
174else
175 cp -R target/${target_dir}/WebRTC.framework "${app_path}/Contents/Frameworks/"
176fi
177
178# Note: The app identifier for our development builds is the same as the app identifier for nightly.
179cp crates/${zed_crate}/contents/$channel/embedded.provisionprofile "${app_path}/Contents/"
180
181if [[ -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
198else
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-build-dmg` 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
217fi
218
219if [[ "$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
227fi
228
229if [ "$local_only" = true ]; then
230 # If bundle_name is not set or empty, use the basename of $app_path
231 if [ -z "$bundle_name" ]; then
232 bundle_name=$(basename "$app_path")
233 else
234 # If bundle_name doesn't end in .app, append it
235 if [[ "$bundle_name" != *.app ]]; then
236 bundle_name="$bundle_name.app"
237 fi
238 fi
239
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
251else
252 echo "Creating DMG"
253 dmg_target_directory="target/${target_dir}"
254 dmg_source_directory="${dmg_target_directory}/dmg"
255 dmg_file_path="${dmg_target_directory}/Zed.dmg"
256
257 rm -rf ${dmg_source_directory}
258 mkdir -p ${dmg_source_directory}
259 mv "${app_path}" "${dmg_source_directory}"
260
261 ln -s /Applications ${dmg_source_directory}
262 hdiutil create -volname Zed -srcfolder "${dmg_source_directory}" -ov -format UDZO "${dmg_file_path}"
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 rm ${dmg_source_directory}/Applications
266
267 echo "Adding license agreement to DMG"
268 npm install --global dmg-license minimist
269 dmg-license script/eula/eula.json "${dmg_file_path}"
270
271 if [[ -n $MACOS_CERTIFICATE && -n $MACOS_CERTIFICATE_PASSWORD && -n $APPLE_NOTARIZATION_USERNAME && -n $APPLE_NOTARIZATION_PASSWORD ]]; then
272 echo "Notarizing DMG with Apple"
273 "$(xcode-select -p)/usr/bin/notarytool" submit --wait --apple-id "$APPLE_NOTARIZATION_USERNAME" --password "$APPLE_NOTARIZATION_PASSWORD" --team-id "$APPLE_NOTORIZATION_TEAM" "${dmg_file_path}"
274 fi
275
276 if [ "$open_result" = true ]; then
277 open $dmg_target_directory
278 fi
279fi