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}/${space_path}/${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 gzip target/aarch64-apple-darwin/release/Zed.dwarf
145 gzip target/x86_64-apple-darwin/release/Zed.dwarf
146 uploadDsym target/aarch64-apple-darwin/release/Zed.dwarf.gz "$channel/Zed-$version-aarch64-apple-darwin.dwarf.gz"
147 uploadDsym target/x86_64-apple-darwin/release/Zed.dwarf.gz "$channel/Zed-$version-x86_64-apple-darwin.dwarf.gz"
148
149 echo "Creating fat binaries"
150 lipo \
151 -create \
152 target/{x86_64-apple-darwin,aarch64-apple-darwin}/${target_dir}/${binary_name} \
153 -output \
154 "${app_path}/Contents/MacOS/${zed_crate}"
155 lipo \
156 -create \
157 target/{x86_64-apple-darwin,aarch64-apple-darwin}/${target_dir}/cli \
158 -output \
159 "${app_path}/Contents/MacOS/cli"
160fi
161
162echo "Copying WebRTC.framework into the frameworks folder"
163mkdir "${app_path}/Contents/Frameworks"
164if [ "$local_arch" = false ]; then
165 cp -R target/${local_target_triple}/${target_dir}/WebRTC.framework "${app_path}/Contents/Frameworks/"
166else
167 cp -R target/${target_dir}/WebRTC.framework "${app_path}/Contents/Frameworks/"
168fi
169
170# Note: The app identifier for our development builds is the same as the app identifier for nightly.
171cp crates/${zed_crate}/contents/$channel/embedded.provisionprofile "${app_path}/Contents/"
172
173if [[ -n $MACOS_CERTIFICATE && -n $MACOS_CERTIFICATE_PASSWORD && -n $APPLE_NOTARIZATION_USERNAME && -n $APPLE_NOTARIZATION_PASSWORD ]]; then
174 echo "Signing bundle with Apple-issued certificate"
175 security create-keychain -p "$MACOS_CERTIFICATE_PASSWORD" zed.keychain || echo ""
176 security default-keychain -s zed.keychain
177 security unlock-keychain -p "$MACOS_CERTIFICATE_PASSWORD" zed.keychain
178 echo "$MACOS_CERTIFICATE" | base64 --decode > /tmp/zed-certificate.p12
179 security import /tmp/zed-certificate.p12 -k zed.keychain -P "$MACOS_CERTIFICATE_PASSWORD" -T /usr/bin/codesign
180 rm /tmp/zed-certificate.p12
181 security set-key-partition-list -S apple-tool:,apple:,codesign: -s -k "$MACOS_CERTIFICATE_PASSWORD" zed.keychain
182
183 # sequence of codesign commands modeled after this example: https://developer.apple.com/forums/thread/701514
184 /usr/bin/codesign --deep --force --timestamp --sign "Zed Industries, Inc." "${app_path}/Contents/Frameworks/WebRTC.framework" -v
185 /usr/bin/codesign --deep --force --timestamp --options runtime --sign "Zed Industries, Inc." "${app_path}/Contents/MacOS/cli" -v
186 /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
187 /usr/bin/codesign --force --timestamp --options runtime --entitlements crates/${zed_crate}/resources/zed.entitlements --sign "Zed Industries, Inc." "${app_path}" -v
188
189 security default-keychain -s login.keychain
190else
191 echo "One or more of the following variables are missing: MACOS_CERTIFICATE, MACOS_CERTIFICATE_PASSWORD, APPLE_NOTARIZATION_USERNAME, APPLE_NOTARIZATION_PASSWORD"
192 if [[ "$local_only" = false ]]; then
193 echo "To create a self-signed local build use ./scripts/build.sh -ldf"
194 exit 1
195 fi
196
197 echo "====== WARNING ======"
198 echo "This bundle is being signed without all entitlements, some features (e.g. universal links) will not work"
199 echo "====== WARNING ======"
200
201 # NOTE: if you need to test universal links you have a few paths forward:
202 # - create a PR and tag it with the `run-build-dmg` label, and download the .dmg file from there.
203 # - get a signing key for the MQ55VZLNZQ team from Nathan.
204 # - create your own signing key, and update references to MQ55VZLNZQ to your own team ID
205 # then comment out this line.
206 cat crates/${zed_crate}/resources/zed.entitlements | sed '/com.apple.developer.associated-domains/,+1d' > "${app_path}/Contents/Resources/zed.entitlements"
207
208 codesign --force --deep --entitlements "${app_path}/Contents/Resources/zed.entitlements" --sign ${MACOS_SIGNING_KEY:- -} "${app_path}" -v
209fi
210
211if [[ "$target_dir" = "debug" && "$local_only" = false ]]; then
212 if [ "$open_result" = true ]; then
213 open "$app_path"
214 else
215 echo "Created application bundle:"
216 echo "$app_path"
217 fi
218 exit 0
219fi
220
221if [ "$local_only" = true ]; then
222 # If bundle_name is not set or empty, use the basename of $app_path
223 if [ -z "$bundle_name" ]; then
224 bundle_name=$(basename "$app_path")
225 else
226 # If bundle_name doesn't end in .app, append it
227 if [[ "$bundle_name" != *.app ]]; then
228 bundle_name="$bundle_name.app"
229 fi
230 fi
231
232 if [ "$overwrite_local_app" = true ]; then
233 rm -rf "/Applications/$bundle_name"
234 fi
235 mv "$app_path" "/Applications/$bundle_name"
236
237 if [ "$open_result" = true ]; then
238 open "/Applications/$bundle_name"
239 else
240 echo "Installed application bundle:"
241 echo "/Applications/$bundle_name"
242 fi
243else
244 echo "Creating DMG"
245 dmg_target_directory="target/${target_dir}"
246 dmg_source_directory="${dmg_target_directory}/dmg"
247 dmg_file_path="${dmg_target_directory}/Zed.dmg"
248
249 rm -rf ${dmg_source_directory}
250 mkdir -p ${dmg_source_directory}
251 mv "${app_path}" "${dmg_source_directory}"
252
253 ln -s /Applications ${dmg_source_directory}
254 hdiutil create -volname Zed -srcfolder "${dmg_source_directory}" -ov -format UDZO "${dmg_file_path}"
255 # If someone runs this bundle script locally, a symlink will be placed in `dmg_source_directory`.
256 # This symlink causes CPU issues with Zed if the Zed codebase is the project being worked on, so we simply remove it for now.
257 rm ${dmg_source_directory}/Applications
258
259 echo "Adding license agreement to DMG"
260 npm install --global dmg-license minimist
261 dmg-license script/eula/eula.json "${dmg_file_path}"
262
263 if [[ -n $MACOS_CERTIFICATE && -n $MACOS_CERTIFICATE_PASSWORD && -n $APPLE_NOTARIZATION_USERNAME && -n $APPLE_NOTARIZATION_PASSWORD ]]; then
264 echo "Notarizing DMG with Apple"
265 "$(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}"
266 fi
267
268 if [ "$open_result" = true ]; then
269 open $dmg_target_directory
270 fi
271fi