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