bundle-mac

  1#!/usr/bin/env bash
  2
  3set -euo pipefail
  4source script/lib/blob-store.sh
  5
  6build_flag="--release"
  7target_dir="release"
  8open_result=false
  9local_install=false
 10can_code_sign=false
 11
 12# This must match the team in the provisioning profile.
 13IDENTITY="Zed Industries, Inc."
 14APPLE_NOTARIZATION_TEAM="MQ55VZLNZQ"
 15
 16# Function for displaying help info
 17help_info() {
 18  echo "
 19Usage: ${0##*/} [options] [architecture=host]
 20Build the application bundle for macOS.
 21
 22Options:
 23  -d    Compile in debug mode
 24  -o    Open dir with the resulting DMG or launch the app itself in local mode.
 25  -i    Install the resulting DMG into /Applications.
 26  -h    Display this help and exit.
 27  "
 28}
 29
 30while getopts 'dloih' flag
 31do
 32    case "${flag}" in
 33        o) open_result=true;;
 34        d)
 35            export CARGO_INCREMENTAL=true
 36            export CARGO_BUNDLE_SKIP_BUILD=true
 37            build_flag="";
 38            target_dir="debug"
 39            ;;
 40        i) local_install=true;;
 41        h)
 42           help_info
 43           exit 0
 44           ;;
 45    esac
 46done
 47
 48shift $((OPTIND-1))
 49
 50
 51# Get release channel
 52pushd crates/zed
 53channel=$(<RELEASE_CHANNEL)
 54export ZED_RELEASE_CHANNEL="${channel}"
 55popd
 56
 57export ZED_BUNDLE=true
 58
 59cargo_bundle_version=$(cargo -q bundle --help 2>&1 | head -n 1 || echo "")
 60if [ "$cargo_bundle_version" != "cargo-bundle v0.6.1-zed" ]; then
 61    cargo install cargo-bundle --git https://github.com/zed-industries/cargo-bundle.git --branch zed-deploy
 62fi
 63
 64# Deal with versions of macOS that don't include libstdc++ headers
 65export CXXFLAGS="-stdlib=libc++"
 66
 67version_info=$(rustc --version --verbose)
 68host_line=$(echo "$version_info" | grep host)
 69target_triple=${host_line#*: }
 70if [[ $# -gt 0 && -n "$1" ]]; then
 71    target_triple="$1"
 72fi
 73arch_suffix=""
 74
 75if [[ "$target_triple" = "x86_64-apple-darwin" ]]; then
 76    arch_suffix="x86_64"
 77elif [[ "$target_triple" = "aarch64-apple-darwin" ]]; then
 78    arch_suffix="aarch64"
 79else
 80    echo "Unsupported architecture $target_triple"
 81    exit 1
 82fi
 83
 84# Generate the licenses first, so they can be baked into the binaries
 85script/generate-licenses
 86
 87rustup target add $target_triple
 88
 89echo "Compiling zed binaries"
 90cargo build ${build_flag} --package zed --package cli --target $target_triple
 91# Build remote_server in separate invocation to prevent feature unification from other crates
 92# from influencing dynamic libraries required by it.
 93cargo build ${build_flag} --package remote_server --target $target_triple
 94
 95echo "Creating application bundle"
 96pushd crates/zed
 97cp Cargo.toml Cargo.toml.backup
 98sed \
 99    -i.backup \
100    "s/package.metadata.bundle-${channel}/package.metadata.bundle/" \
101    Cargo.toml
102
103app_path=$(cargo bundle ${build_flag} --target $target_triple --select-workspace-root | xargs)
104
105mv Cargo.toml.backup Cargo.toml
106popd
107echo "Bundled ${app_path}"
108
109if [[ -n "${MACOS_CERTIFICATE:-}" && -n "${MACOS_CERTIFICATE_PASSWORD:-}" && -n "${APPLE_NOTARIZATION_KEY:-}" && -n "${APPLE_NOTARIZATION_KEY_ID:-}" && -n "${APPLE_NOTARIZATION_ISSUER_ID:-}" ]]; then
110    can_code_sign=true
111
112    echo "Setting up keychain for code signing..."
113    security create-keychain -p "$MACOS_CERTIFICATE_PASSWORD" zed.keychain || echo ""
114    security default-keychain -s zed.keychain
115    security unlock-keychain -p "$MACOS_CERTIFICATE_PASSWORD" zed.keychain
116    # Calling set-keychain-settings without `-t` disables the auto-lock timeout
117    security set-keychain-settings zed.keychain
118    echo "$MACOS_CERTIFICATE" | base64 --decode > /tmp/zed-certificate.p12
119    security import /tmp/zed-certificate.p12 -k zed.keychain -P "$MACOS_CERTIFICATE_PASSWORD" -T /usr/bin/codesign
120    rm /tmp/zed-certificate.p12
121    security set-key-partition-list -S apple-tool:,apple:,codesign: -s -k "$MACOS_CERTIFICATE_PASSWORD" zed.keychain
122
123    function cleanup() {
124        echo "Cleaning up keychain"
125        security default-keychain -s login.keychain
126        security delete-keychain zed.keychain
127    }
128
129    trap cleanup EXIT
130fi
131
132GIT_VERSION="v2.43.3"
133GIT_VERSION_SHA="fa29823"
134
135function download_and_unpack() {
136    local url=$1
137    local path_to_unpack=$2
138    local target_path=$3
139
140    temp_dir=$(mktemp -d)
141
142    if ! command -v curl &> /dev/null; then
143        echo "curl is not installed. Please install curl to continue."
144        exit 1
145    fi
146
147    curl --silent --fail --location "$url" | tar -xvz -C "$temp_dir" -f - $path_to_unpack
148
149    mv "$temp_dir/$path_to_unpack" "$target_path"
150
151    rm -rf "$temp_dir"
152}
153
154function download_git() {
155    local architecture=$1
156    local target_binary=$2
157
158    tmp_dir=$(mktemp -d)
159    pushd "$tmp_dir"
160
161    case "$architecture" in
162        aarch64-apple-darwin)
163            download_and_unpack "https://github.com/desktop/dugite-native/releases/download/${GIT_VERSION}/dugite-native-${GIT_VERSION}-${GIT_VERSION_SHA}-macOS-arm64.tar.gz" bin/git ./git
164            ;;
165        x86_64-apple-darwin)
166            download_and_unpack "https://github.com/desktop/dugite-native/releases/download/${GIT_VERSION}/dugite-native-${GIT_VERSION}-${GIT_VERSION_SHA}-macOS-x64.tar.gz" bin/git ./git
167            ;;
168        *)
169            echo "Unsupported architecture: $architecture"
170            exit 1
171            ;;
172    esac
173
174    popd
175
176    mv "${tmp_dir}/git" "${target_binary}"
177    rm -rf "$tmp_dir"
178}
179
180function sign_app_binaries() {
181    rm -rf "${app_path}/Contents/Frameworks"
182    mkdir -p "${app_path}/Contents/Frameworks"
183
184    echo "Downloading git binary"
185    download_git "${target_triple}" "${app_path}/Contents/MacOS/git"
186
187    # Note: The app identifier for our development builds is the same as the app identifier for nightly.
188    cp crates/zed/contents/$channel/embedded.provisionprofile "${app_path}/Contents/"
189
190    if [[ $can_code_sign = true ]]; then
191        echo "Code signing binaries"
192        # sequence of codesign commands modeled after this example: https://developer.apple.com/forums/thread/701514
193        /usr/bin/codesign --deep --force --timestamp --options runtime --sign "$IDENTITY" "${app_path}/Contents/MacOS/cli" -v
194        /usr/bin/codesign --deep --force --timestamp --options runtime --sign "$IDENTITY" "${app_path}/Contents/MacOS/git" -v
195        /usr/bin/codesign --deep --force --timestamp --options runtime --entitlements crates/zed/resources/zed.entitlements --sign "$IDENTITY" "${app_path}/Contents/MacOS/zed" -v
196        /usr/bin/codesign --force --timestamp --options runtime --entitlements crates/zed/resources/zed.entitlements --sign "$IDENTITY" "${app_path}" -v
197    else
198        echo "One or more of the following variables are missing: MACOS_CERTIFICATE, MACOS_CERTIFICATE_PASSWORD, APPLE_NOTARIZATION_KEY, APPLE_NOTARIZATION_KEY_ID, APPLE_NOTARIZATION_ISSUER_ID"
199
200        echo "====== WARNING ======"
201        echo "This bundle is being signed without all entitlements, some features (e.g. universal links) will not work"
202        echo "====== WARNING ======"
203
204        # NOTE: if you need to test universal links you have a few paths forward:
205        # - create a PR and tag it with the `run-bundling` label, and download the .dmg file from there.
206        # - get a signing key for the MQ55VZLNZQ team from Nathan.
207        # - create your own signing key, and update references to MQ55VZLNZQ to your own team ID
208        # then comment out this line.
209        cat crates/zed/resources/zed.entitlements | sed '/com.apple.developer.associated-domains/,+1d' > "${app_path}/Contents/Resources/zed.entitlements"
210
211        codesign --force --deep --entitlements "${app_path}/Contents/Resources/zed.entitlements" --sign ${MACOS_SIGNING_KEY:- -} "${app_path}" -v
212    fi
213
214    bundle_name=$(basename "$app_path")
215
216    if [ "$local_install" = true ]; then
217        rm -rf "/Applications/$bundle_name"
218        mv "$app_path" "/Applications/$bundle_name"
219        echo "Installed application bundle: /Applications/$bundle_name"
220        if [ "$open_result" = true ]; then
221            echo "Opening /Applications/$bundle_name"
222            open "/Applications/$bundle_name"
223        fi
224    elif [ "$open_result" = true ]; then
225        open "$app_path"
226    fi
227
228    if [[ "$target_dir" = "debug" ]]; then
229        echo "Debug build detected - skipping DMG creation and signing"
230        if [ "$local_install" = false ]; then
231            echo "Created application bundle:"
232            echo "$app_path"
233        fi
234    else
235        dmg_target_directory="target/${target_triple}/${target_dir}"
236        dmg_source_directory="${dmg_target_directory}/dmg"
237        dmg_file_path="${dmg_target_directory}/Zed-${arch_suffix}.dmg"
238        xcode_bin_dir_path="$(xcode-select -p)/usr/bin"
239
240        rm -rf ${dmg_source_directory}
241        mkdir -p ${dmg_source_directory}
242        mv "${app_path}" "${dmg_source_directory}"
243        notarization_key_file=$(mktemp)
244
245        echo "Adding symlink to /Applications to ${dmg_source_directory}"
246        ln -s /Applications ${dmg_source_directory}
247
248        echo "Creating final DMG at ${dmg_file_path} using ${dmg_source_directory}"
249        hdiutil create -volname Zed -srcfolder "${dmg_source_directory}" -ov -format UDZO "${dmg_file_path}"
250
251        # If someone runs this bundle script locally, a symlink will be placed in `dmg_source_directory`.
252        # This symlink causes CPU issues with Zed if the Zed codebase is the project being worked on, so we simply remove it for now.
253        echo "Removing symlink to /Applications from ${dmg_source_directory}"
254        rm ${dmg_source_directory}/Applications
255
256        echo "Adding license agreement to DMG"
257        npm install --global dmg-license minimist
258        dmg-license script/terms/terms.json "${dmg_file_path}"
259
260        if [[ $can_code_sign = true ]]; then
261            echo "Notarizing DMG with Apple"
262            /usr/bin/codesign --deep --force --timestamp --options runtime --sign "$IDENTITY" "$(pwd)/${dmg_file_path}" -v
263            echo "$APPLE_NOTARIZATION_KEY" > "$notarization_key_file"
264            "${xcode_bin_dir_path}/notarytool" submit --wait --key "$notarization_key_file" --key-id "$APPLE_NOTARIZATION_KEY_ID" --issuer "$APPLE_NOTARIZATION_ISSUER_ID" "${dmg_file_path}"
265            rm "$notarization_key_file"
266            "${xcode_bin_dir_path}/stapler" staple "${dmg_file_path}"
267        fi
268
269        if [ "$open_result" = true ]; then
270            open $dmg_target_directory
271        fi
272    fi
273}
274
275function sign_binary() {
276    local binary_path=$1
277
278    if [[ $can_code_sign = true ]]; then
279        echo "Code signing executable $binary_path"
280        /usr/bin/codesign --deep --force --timestamp --options runtime --entitlements crates/zed/resources/zed.entitlements --sign "$IDENTITY" "${binary_path}" -v
281    fi
282}
283
284function upload_debug_symbols() {
285    if [ "$local_install" = true ]; then
286        echo "local install; skipping sentry upload."
287    elif [[ -n "${SENTRY_AUTH_TOKEN:-}" ]]; then
288        echo "Uploading zed debug symbols to sentry..."
289        exe_path="target/${target_triple}/release/Zed"
290        if ! dsymutil --flat "target/${target_triple}/${target_dir}/zed" 2> target/dsymutil.log; then
291            echo "dsymutil failed"
292            cat target/dsymutil.log
293            exit 1
294        fi
295        if ! dsymutil --flat "target/${target_triple}/${target_dir}/remote_server" 2> target/dsymutil.log; then
296            echo "dsymutil failed"
297            cat target/dsymutil.log
298            exit 1
299        fi
300        # note: this uploads the unstripped binary which is needed because it contains
301        # .eh_frame data for stack unwinding. see https://github.com/getsentry/symbolic/issues/783
302        sentry-cli debug-files upload --include-sources --wait -p zed -o zed-dev \
303        # Try uploading up to 3 times
304        for attempt in 1 2 3; do
305            echo "Sentry upload attempt $attempt..."
306            if sentry-cli debug-files upload --include-sources --wait -p zed -o zed-dev \
307                "target/${target_triple}/${target_dir}/zed.dwarf" \
308                "target/${target_triple}/${target_dir}/remote_server.dwarf"; then
309                break
310            else
311                echo "Sentry upload failed on attempt $attempt"
312                if [ $attempt -eq 3 ]; then
313                    echo "All sentry upload attempts failed"
314                    exit 1
315                fi
316            fi
317        done
318    else
319        echo "missing SENTRY_AUTH_TOKEN. skipping sentry upload."
320    fi
321}
322
323upload_debug_symbols
324
325cp target/${target_triple}/${target_dir}/zed "${app_path}/Contents/MacOS/zed"
326cp target/${target_triple}/${target_dir}/cli "${app_path}/Contents/MacOS/cli"
327sign_app_binaries
328
329sign_binary "target/$target_triple/release/remote_server"
330gzip -f --stdout --best target/$target_triple/release/remote_server > target/zed-remote-server-macos-$arch_suffix.gz