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