bundle-mac

  1#!/usr/bin/env bash
  2
  3set -euxo pipefail
  4source script/lib/blob-store.sh
  5
  6build_flag="--release"
  7target_dir="release"
  8open_result=false
  9local_arch=false
 10local_only=false
 11overwrite_local_app=false
 12bundle_name=""
 13zed_crate="zed"
 14binary_name="Zed"
 15
 16# This must match the team in the provisioning profile.
 17APPLE_NOTORIZATION_TEAM="MQ55VZLNZQ"
 18
 19# Function for displaying help info
 20help_info() {
 21  echo "
 22Usage: ${0##*/} [options] [bundle_name]
 23Build the application bundle for macOS.
 24
 25Options:
 26  -d    Compile in debug mode
 27  -l    Compile for local architecture and copy bundle to /Applications, implies -d.
 28  -o    Open the resulting DMG or the app itself in local mode.
 29  -f    Overwrite the local app bundle if it exists.
 30  -h    Display this help and exit.
 31  "
 32}
 33
 34while getopts 'dlfoh' 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        h)
 54           help_info
 55           exit 0
 56           ;;
 57    esac
 58done
 59
 60shift $((OPTIND-1))
 61
 62if [[ $# -gt 0 ]]; then
 63    if [ "$1" ]; then
 64        bundle_name=$1
 65    fi
 66fi
 67
 68export ZED_BUNDLE=true
 69export MACOSX_DEPLOYMENT_TARGET=10.15.7
 70
 71cargo_bundle_version=$(cargo -q bundle --help 2>&1 | head -n 1 || echo "")
 72if [ "$cargo_bundle_version" != "cargo-bundle v0.6.0-zed" ]; then
 73    cargo install cargo-bundle --git https://github.com/zed-industries/cargo-bundle.git --branch zed-deploy
 74fi
 75
 76# Deal with versions of macOS that don't include libstdc++ headers
 77export CXXFLAGS="-stdlib=libc++"
 78
 79version_info=$(rustc --version --verbose)
 80host_line=$(echo "$version_info" | grep host)
 81local_target_triple=${host_line#*: }
 82
 83if [ "$local_arch" = true ]; then
 84    echo "Building for local target only."
 85    cargo build ${build_flag} --package ${zed_crate} --package cli
 86else
 87    echo "Compiling zed binaries"
 88    cargo build ${build_flag} --package ${zed_crate} --package cli --target aarch64-apple-darwin --target x86_64-apple-darwin
 89fi
 90
 91echo "Creating application bundle"
 92pushd crates/zed
 93channel=$(<RELEASE_CHANNEL)
 94popd
 95
 96pushd crates/${zed_crate}
 97cp Cargo.toml Cargo.toml.backup
 98sed \
 99    -i .backup \
100    "s/package.metadata.bundle-${channel}/package.metadata.bundle/" \
101    Cargo.toml
102
103if [ "$local_arch" = true ]; then
104    app_path=$(cargo bundle ${build_flag} --select-workspace-root | xargs)
105else
106    app_path_x64=$(cargo bundle ${build_flag} --target x86_64-apple-darwin --select-workspace-root | xargs)
107    app_path_aarch64=$(cargo bundle ${build_flag} --target aarch64-apple-darwin --select-workspace-root | xargs)
108    app_path=$app_path_x64
109fi
110
111mv Cargo.toml.backup Cargo.toml
112popd
113echo "Bundled ${app_path}"
114
115function prepare_binaries() {
116    local architecture=$1
117    local app_path=$2
118
119    echo "Uploading dSYMs for $architecture"
120    dsymutil --flat target/${architecture}/${target_dir}/Zed
121    version="$(cargo metadata --no-deps --manifest-path crates/zed/Cargo.toml --offline --format-version=1 | jq -r '.packages | map(select(.name == "zed"))[0].version')"
122    if [ "$channel" == "nightly" ]; then
123        version="$version-$(git rev-parse --short HEAD)"
124    fi
125
126    echo "Removing existing gzipped dSYMs for $architecture"
127    rm -f target/${architecture}/${target_dir}/Zed.dwarf.gz
128
129    echo "Gzipping dSYMs for $architecture"
130    gzip target/${architecture}/${target_dir}/Zed.dwarf
131
132    echo "Uploading dSYMs for $architecture"
133    upload_to_blob_store_public \
134        "zed-debug-symbols" \
135        target/${architecture}/${target_dir}/Zed.dwarf.gz \
136        "$channel/Zed-$version-${architecture}.dwarf.gz"
137
138    cp target/${architecture}/${target_dir}/${binary_name} "${app_path}/Contents/MacOS/${zed_crate}"
139    cp target/${architecture}/${target_dir}/cli "${app_path}/Contents/MacOS/cli"
140}
141
142if [ "$local_arch" = false ]; then
143    prepare_binaries "aarch64-apple-darwin" "$app_path_aarch64"
144    prepare_binaries "x86_64-apple-darwin" "$app_path_x64"
145fi
146
147function sign_binaries() {
148    local app_path=$1
149    local architecture_dir=$2
150    echo "Copying WebRTC.framework into the frameworks folder"
151    mkdir "${app_path}/Contents/Frameworks"
152    if [ "$local_arch" = false ]; then
153        cp -R target/${local_target_triple}/${target_dir}/WebRTC.framework "${app_path}/Contents/Frameworks/"
154    else
155        cp -R target/${architecture_dir}/${target_dir}/WebRTC.framework "${app_path}/Contents/Frameworks/"
156        cp -R target/${architecture_dir}/${target_dir}/cli "${app_path}/Contents/MacOS/"
157    fi
158
159    # Note: The app identifier for our development builds is the same as the app identifier for nightly.
160    cp crates/${zed_crate}/contents/$channel/embedded.provisionprofile "${app_path}/Contents/"
161
162    if [[ -n "${MACOS_CERTIFICATE:-}" && -n "${MACOS_CERTIFICATE_PASSWORD:-}" && -n "${APPLE_NOTARIZATION_USERNAME:-}" && -n "${APPLE_NOTARIZATION_PASSWORD:-}" ]]; then
163        echo "Signing bundle with Apple-issued certificate"
164        security create-keychain -p "$MACOS_CERTIFICATE_PASSWORD" zed.keychain || echo ""
165        security default-keychain -s zed.keychain
166        security unlock-keychain -p "$MACOS_CERTIFICATE_PASSWORD" zed.keychain
167        echo "$MACOS_CERTIFICATE" | base64 --decode > /tmp/zed-certificate.p12
168        security import /tmp/zed-certificate.p12 -k zed.keychain -P "$MACOS_CERTIFICATE_PASSWORD" -T /usr/bin/codesign
169        rm /tmp/zed-certificate.p12
170        security set-key-partition-list -S apple-tool:,apple:,codesign: -s -k "$MACOS_CERTIFICATE_PASSWORD" zed.keychain
171
172        # sequence of codesign commands modeled after this example: https://developer.apple.com/forums/thread/701514
173        /usr/bin/codesign --deep --force --timestamp --sign "Zed Industries, Inc." "${app_path}/Contents/Frameworks/WebRTC.framework" -v
174        /usr/bin/codesign --deep --force --timestamp --options runtime --sign "Zed Industries, Inc." "${app_path}/Contents/MacOS/cli" -v
175        /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
176        /usr/bin/codesign --force --timestamp --options runtime --entitlements crates/${zed_crate}/resources/zed.entitlements --sign "Zed Industries, Inc." "${app_path}" -v
177
178        security default-keychain -s login.keychain
179    else
180        echo "One or more of the following variables are missing: MACOS_CERTIFICATE, MACOS_CERTIFICATE_PASSWORD, APPLE_NOTARIZATION_USERNAME, APPLE_NOTARIZATION_PASSWORD"
181        if [[ "$local_only" = false ]]; then
182            echo "To create a self-signed local build use ./scripts/build.sh -ldf"
183            exit 1
184        fi
185
186        echo "====== WARNING ======"
187        echo "This bundle is being signed without all entitlements, some features (e.g. universal links) will not work"
188        echo "====== WARNING ======"
189
190        # NOTE: if you need to test universal links you have a few paths forward:
191        # - create a PR and tag it with the `run-bundling` label, and download the .dmg file from there.
192        # - get a signing key for the MQ55VZLNZQ team from Nathan.
193        # - create your own signing key, and update references to MQ55VZLNZQ to your own team ID
194        # then comment out this line.
195        cat crates/${zed_crate}/resources/zed.entitlements | sed '/com.apple.developer.associated-domains/,+1d' > "${app_path}/Contents/Resources/zed.entitlements"
196
197        codesign --force --deep --entitlements "${app_path}/Contents/Resources/zed.entitlements" --sign ${MACOS_SIGNING_KEY:- -} "${app_path}" -v
198    fi
199
200    if [[ "$target_dir" = "debug" && "$local_only" = false ]]; then
201        if [ "$open_result" = true ]; then
202            open "$app_path"
203        else
204            echo "Created application bundle:"
205            echo "$app_path"
206        fi
207        exit 0
208    fi
209
210    # If bundle_name is not set or empty, use the basename of $app_path
211    if [ -z "$bundle_name" ]; then
212        bundle_name=$(basename "$app_path")
213    else
214        # If bundle_name doesn't end in .app, append it
215        if [[ "$bundle_name" != *.app ]]; then
216            bundle_name="$bundle_name.app"
217        fi
218    fi
219
220    if [ "$local_only" = true ]; then
221        if [ "$overwrite_local_app" = true ]; then
222            rm -rf "/Applications/$bundle_name"
223        fi
224        mv "$app_path" "/Applications/$bundle_name"
225
226        if [ "$open_result" = true ]; then
227            open "/Applications/$bundle_name"
228        else
229            echo "Installed application bundle:"
230            echo "/Applications/$bundle_name"
231        fi
232    else
233        dmg_target_directory="target/${architecture_dir}/${target_dir}"
234        dmg_source_directory="${dmg_target_directory}/dmg"
235        dmg_file_path="${dmg_target_directory}/Zed.dmg"
236        xcode_bin_dir_path="$(xcode-select -p)/usr/bin"
237
238        rm -rf ${dmg_source_directory}
239        mkdir -p ${dmg_source_directory}
240        mv "${app_path}" "${dmg_source_directory}"
241
242        if [[ -n $MACOS_CERTIFICATE && -n $MACOS_CERTIFICATE_PASSWORD && -n $APPLE_NOTARIZATION_USERNAME && -n $APPLE_NOTARIZATION_PASSWORD ]]; then
243            echo "Creating temporary DMG at ${dmg_file_path} using ${dmg_source_directory} to notarize app bundle"
244            hdiutil create -volname Zed -srcfolder "${dmg_source_directory}" -ov -format UDZO "${dmg_file_path}"
245
246            security create-keychain -p "$MACOS_CERTIFICATE_PASSWORD" zed.keychain || echo ""
247            security default-keychain -s zed.keychain
248            security unlock-keychain -p "$MACOS_CERTIFICATE_PASSWORD" zed.keychain
249            echo "$MACOS_CERTIFICATE" | base64 --decode > /tmp/zed-certificate.p12
250            security import /tmp/zed-certificate.p12 -k zed.keychain -P "$MACOS_CERTIFICATE_PASSWORD" -T /usr/bin/codesign
251            rm /tmp/zed-certificate.p12
252            security set-key-partition-list -S apple-tool:,apple:,codesign: -s -k "$MACOS_CERTIFICATE_PASSWORD" zed.keychain
253
254            /usr/bin/codesign --deep --force --timestamp --options runtime --sign "Zed Industries, Inc." "$(pwd)/${dmg_file_path}" -v
255            security default-keychain -s login.keychain
256            echo "Notarizing DMG with Apple"
257            "${xcode_bin_dir_path}/notarytool" submit --wait --apple-id "$APPLE_NOTARIZATION_USERNAME" --password "$APPLE_NOTARIZATION_PASSWORD" --team-id "$APPLE_NOTORIZATION_TEAM" "${dmg_file_path}"
258
259            echo "Removing temporary DMG (used only for notarization)"
260            rm "${dmg_file_path}"
261
262            echo "Stapling notarization ticket to ${dmg_source_directory}/${bundle_name}"
263            "${xcode_bin_dir_path}/stapler" staple "${dmg_source_directory}/${bundle_name}"
264        fi
265
266        echo "Adding symlink to /Applications to ${dmg_source_directory}"
267        ln -s /Applications ${dmg_source_directory}
268
269        echo "Creating final DMG at ${dmg_file_path} using ${dmg_source_directory}"
270        hdiutil create -volname Zed -srcfolder "${dmg_source_directory}" -ov -format UDZO "${dmg_file_path}"
271
272        # If someone runs this bundle script locally, a symlink will be placed in `dmg_source_directory`.
273        # This symlink causes CPU issues with Zed if the Zed codebase is the project being worked on, so we simply remove it for now.
274        echo "Removing symlink to /Applications from ${dmg_source_directory}"
275        rm ${dmg_source_directory}/Applications
276
277        echo "Adding license agreement to DMG"
278        npm install --global dmg-license minimist
279        dmg-license script/eula/eula.json "${dmg_file_path}"
280
281        if [[ -n $MACOS_CERTIFICATE && -n $MACOS_CERTIFICATE_PASSWORD && -n $APPLE_NOTARIZATION_USERNAME && -n $APPLE_NOTARIZATION_PASSWORD ]]; then
282            echo "Notarizing DMG with Apple"
283            security create-keychain -p "$MACOS_CERTIFICATE_PASSWORD" zed.keychain || echo ""
284            security default-keychain -s zed.keychain
285            security unlock-keychain -p "$MACOS_CERTIFICATE_PASSWORD" zed.keychain
286            echo "$MACOS_CERTIFICATE" | base64 --decode > /tmp/zed-certificate.p12
287            security import /tmp/zed-certificate.p12 -k zed.keychain -P "$MACOS_CERTIFICATE_PASSWORD" -T /usr/bin/codesign
288            rm /tmp/zed-certificate.p12
289            security set-key-partition-list -S apple-tool:,apple:,codesign: -s -k "$MACOS_CERTIFICATE_PASSWORD" zed.keychain
290            /usr/bin/codesign --deep --force --timestamp --options runtime --sign "Zed Industries, Inc." "$(pwd)/${dmg_file_path}" -v
291            security default-keychain -s login.keychain
292            "${xcode_bin_dir_path}/notarytool" submit --wait --apple-id "$APPLE_NOTARIZATION_USERNAME" --password "$APPLE_NOTARIZATION_PASSWORD" --team-id "$APPLE_NOTORIZATION_TEAM" "${dmg_file_path}"
293            "${xcode_bin_dir_path}/stapler" staple "${dmg_file_path}"
294        fi
295
296        if [ "$open_result" = true ]; then
297            open $dmg_target_directory
298        fi
299    fi
300}
301
302if [ "$local_arch" = true ]; then
303    sign_binaries "$app_path" "$local_target_triple"
304else
305    # Create universal binary
306    cp -R "$app_path_x64" target/release/
307    app_path=target/release/$(basename "$app_path_x64")
308    lipo \
309        -create \
310        target/{x86_64-apple-darwin,aarch64-apple-darwin}/${target_dir}/${binary_name} \
311        -output \
312        "${app_path}/Contents/MacOS/${zed_crate}"
313    lipo \
314        -create \
315        target/{x86_64-apple-darwin,aarch64-apple-darwin}/${target_dir}/cli \
316        -output \
317        "${app_path}/Contents/MacOS/cli"
318    sign_binaries "$app_path" "."
319
320    sign_binaries "$app_path_x64" "x86_64-apple-darwin"
321    sign_binaries "$app_path_aarch64" "aarch64-apple-darwin"
322fi