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 provsiioning 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 -2 Build zed 2 instead of zed 1.
31 "
32}
33
34while getopts 'dlfoh2' flag
35do
36 case "${flag}" in
37 o) open_result=true;;
38 d)
39 export CARGO_INCREMENTAL=true
40 export CARGO_BUNDLE_SKIP_BUILD=true
41 build_flag="";
42 target_dir="debug"
43 ;;
44 l)
45 export CARGO_INCREMENTAL=true
46 export CARGO_BUNDLE_SKIP_BUILD=true
47 build_flag=""
48 local_arch=true
49 local_only=true
50 target_dir="debug"
51 ;;
52 f) overwrite_local_app=true;;
53 2)
54 zed_crate="zed2"
55 binary_name="Zed2"
56 ;;
57 h)
58 help_info
59 exit 0
60 ;;
61 esac
62done
63
64shift $((OPTIND-1))
65
66if [ "$1" ]; then
67 bundle_name=$1
68fi
69
70export ZED_BUNDLE=true
71export MACOSX_DEPLOYMENT_TARGET=10.15.7
72
73cargo_bundle_version=$(cargo -q bundle --help 2>&1 | head -n 1 || echo "")
74if [ "$cargo_bundle_version" != "cargo-bundle v0.6.0-zed" ]; then
75 cargo install cargo-bundle --git https://github.com/zed-industries/cargo-bundle.git --branch zed-deploy
76fi
77
78rustup target add wasm32-wasi
79
80# Deal with versions of macOS that don't include libstdc++ headers
81export CXXFLAGS="-stdlib=libc++"
82
83version_info=$(rustc --version --verbose)
84host_line=$(echo "$version_info" | grep host)
85local_target_triple=${host_line#*: }
86
87if [ "$local_arch" = true ]; then
88 echo "Building for local target only."
89 cargo build ${build_flag} --package ${zed_crate}
90 cargo build ${build_flag} --package cli
91else
92 echo "Compiling zed binaries"
93 cargo build ${build_flag} --package ${zed_crate} --package cli --target aarch64-apple-darwin --target x86_64-apple-darwin
94fi
95
96echo "Creating application bundle"
97pushd crates/zed
98channel=$(<RELEASE_CHANNEL)
99popd
100
101pushd crates/${zed_crate}
102cp Cargo.toml Cargo.toml.backup
103sed \
104 -i .backup \
105 "s/package.metadata.bundle-${channel}/package.metadata.bundle/" \
106 Cargo.toml
107
108if [ "$local_arch" = true ]; then
109 app_path=$(cargo bundle ${build_flag} --select-workspace-root | xargs)
110else
111 app_path=$(cargo bundle ${build_flag} --target x86_64-apple-darwin --select-workspace-root | xargs)
112fi
113
114mv Cargo.toml.backup Cargo.toml
115popd
116echo "Bundled ${app_path}"
117
118if [ "$local_arch" = false ]; then
119 echo "Creating fat binaries"
120 lipo \
121 -create \
122 target/{x86_64-apple-darwin,aarch64-apple-darwin}/${target_dir}/${binary_name} \
123 -output \
124 "${app_path}/Contents/MacOS/${zed_crate}"
125 lipo \
126 -create \
127 target/{x86_64-apple-darwin,aarch64-apple-darwin}/${target_dir}/cli \
128 -output \
129 "${app_path}/Contents/MacOS/cli"
130fi
131
132echo "Copying WebRTC.framework into the frameworks folder"
133mkdir "${app_path}/Contents/Frameworks"
134if [ "$local_arch" = false ]; then
135 cp -R target/${local_target_triple}/${target_dir}/WebRTC.framework "${app_path}/Contents/Frameworks/"
136else
137 cp -R target/${target_dir}/WebRTC.framework "${app_path}/Contents/Frameworks/"
138fi
139
140#todo!(The app identifier has been set to 'Dev', but the channel is nightly, RATIONALIZE ALL OF THIS MESS)
141cp crates/${zed_crate}/contents/$channel/embedded.provisionprofile "${app_path}/Contents/"
142
143if [[ -n $MACOS_CERTIFICATE && -n $MACOS_CERTIFICATE_PASSWORD && -n $APPLE_NOTARIZATION_USERNAME && -n $APPLE_NOTARIZATION_PASSWORD ]]; then
144 echo "Signing bundle with Apple-issued certificate"
145 security create-keychain -p "$MACOS_CERTIFICATE_PASSWORD" zed.keychain || echo ""
146 security default-keychain -s zed.keychain
147 security unlock-keychain -p "$MACOS_CERTIFICATE_PASSWORD" zed.keychain
148 echo "$MACOS_CERTIFICATE" | base64 --decode > /tmp/zed-certificate.p12
149 security import /tmp/zed-certificate.p12 -k zed.keychain -P "$MACOS_CERTIFICATE_PASSWORD" -T /usr/bin/codesign
150 rm /tmp/zed-certificate.p12
151 security set-key-partition-list -S apple-tool:,apple:,codesign: -s -k "$MACOS_CERTIFICATE_PASSWORD" zed.keychain
152
153 # sequence of codesign commands modeled after this example: https://developer.apple.com/forums/thread/701514
154 /usr/bin/codesign --deep --force --timestamp --sign "Zed Industries, Inc." "${app_path}/Contents/Frameworks/WebRTC.framework" -v
155
156 # todo!(restore cli to zed2)
157 if [[ "$zed_crate" == "zed" ]]; then
158 /usr/bin/codesign --deep --force --timestamp --options runtime --sign "Zed Industries, Inc." "${app_path}/Contents/MacOS/cli" -v
159 fi
160
161 /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
162 /usr/bin/codesign --force --timestamp --options runtime --entitlements crates/${zed_crate}/resources/zed.entitlements --sign "Zed Industries, Inc." "${app_path}" -v
163
164 security default-keychain -s login.keychain
165else
166 echo "One or more of the following variables are missing: MACOS_CERTIFICATE, MACOS_CERTIFICATE_PASSWORD, APPLE_NOTARIZATION_USERNAME, APPLE_NOTARIZATION_PASSWORD"
167 if [[ "$local_only" = false ]]; then
168 echo "To create a self-signed local build use ./scripts/build.sh -ldf"
169 exit 1
170 fi
171
172 echo "====== WARNING ======"
173 echo "This bundle is being signed without all entitlements, some features (e.g. universal links) will not work"
174 echo "====== WARNING ======"
175
176 # NOTE: if you need to test universal links you have a few paths forward:
177 # - create a PR and tag it with the `run-build-dmg` label, and download the .dmg file from there.
178 # - get a signing key for the MQ55VZLNZQ team from Nathan.
179 # - create your own signing key, and update references to MQ55VZLNZQ to your own team ID
180 # then comment out this line.
181 cat crates/${zed_crate}/resources/zed.entitlements | sed '/com.apple.developer.associated-domains/,+1d' > "${app_path}/Contents/Resources/zed.entitlements"
182
183 codesign --force --deep --entitlements "${app_path}/Contents/Resources/zed.entitlements" --sign ${MACOS_SIGNING_KEY:- -} "${app_path}" -v
184fi
185
186if [[ "$target_dir" = "debug" && "$local_only" = false ]]; then
187 if [ "$open_result" = true ]; then
188 open "$app_path"
189 else
190 echo "Created application bundle:"
191 echo "$app_path"
192 fi
193 exit 0
194fi
195
196if [ "$local_only" = true ]; then
197 # If bundle_name is not set or empty, use the basename of $app_path
198 if [ -z "$bundle_name" ]; then
199 bundle_name=$(basename "$app_path")
200 else
201 # If bundle_name doesn't end in .app, append it
202 if [[ "$bundle_name" != *.app ]]; then
203 bundle_name="$bundle_name.app"
204 fi
205 fi
206
207 if [ "$overwrite_local_app" = true ]; then
208 rm -rf "/Applications/$bundle_name"
209 fi
210 mv "$app_path" "/Applications/$bundle_name"
211
212 if [ "$open_result" = true ]; then
213 open "/Applications/$bundle_name"
214 else
215 echo "Installed application bundle:"
216 echo "/Applications/$bundle_name"
217 fi
218else
219 echo "Creating DMG"
220 dmg_target_directory="target/${target_dir}"
221 dmg_source_directory="${dmg_target_directory}/dmg"
222 dmg_file_path="${dmg_target_directory}/Zed.dmg"
223
224 rm -rf ${dmg_source_directory}
225 mkdir -p ${dmg_source_directory}
226 mv "${app_path}" "${dmg_source_directory}"
227
228 ln -s /Applications ${dmg_source_directory}
229 hdiutil create -volname Zed -srcfolder "${dmg_source_directory}" -ov -format UDZO "${dmg_file_path}"
230 # If someone runs this bundle script locally, a symlink will be placed in `dmg_source_directory`.
231 # This symlink causes CPU issues with Zed if the Zed codebase is the project being worked on, so we simply remove it for now.
232 rm ${dmg_source_directory}/Applications
233
234 echo "Adding license agreement to DMG"
235 npm install --global dmg-license minimist
236 dmg-license script/eula/eula.json "${dmg_file_path}"
237
238 if [[ -n $MACOS_CERTIFICATE && -n $MACOS_CERTIFICATE_PASSWORD && -n $APPLE_NOTARIZATION_USERNAME && -n $APPLE_NOTARIZATION_PASSWORD ]]; then
239 echo "Notarizing DMG with Apple"
240 "$(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}"
241 fi
242
243 if [ "$open_result" = true ]; then
244 open $dmg_target_directory
245 fi
246fi