build-mac

  1#!/usr/bin/env bash
  2
  3set -euo pipefail
  4source script/lib/blob-store.sh
  5
  6build_flag="--release"
  7target_dir="release"
  8local_arch=false
  9local_only=false
 10local_install=false
 11bundle_name=""
 12
 13# This must match the team in the provisioning profile.
 14IDENTITY="Zed Industries, Inc."
 15APPLE_NOTARIZATION_TEAM="MQ55VZLNZQ"
 16
 17# Function for displaying help info
 18help_info() {
 19  echo "
 20Usage: ${0##*/} [options] [bundle_name]
 21Build the application bundle for macOS.
 22
 23Options:
 24  -d    Compile in debug mode
 25  -l    Compile for local architecture only.
 26  -i    Install the resulting DMG into /Applications in local mode. Noop without -l.
 27  -h    Display this help and exit.
 28  "
 29}
 30
 31function prepare_binaries() {
 32    local architecture=$1
 33    local app_path=$2
 34
 35    cp target/${architecture}/${target_dir}/zed "${app_path}/Contents/MacOS/zed"
 36    cp target/${architecture}/${target_dir}/cli "${app_path}/Contents/MacOS/cli"
 37}
 38
 39while getopts 'dlih' flag
 40do
 41    case "${flag}" in
 42        d)
 43            export CARGO_INCREMENTAL=true
 44            export CARGO_BUNDLE_SKIP_BUILD=true
 45            build_flag="";
 46            target_dir="debug"
 47            ;;
 48        l)
 49            export CARGO_INCREMENTAL=true
 50            export CARGO_BUNDLE_SKIP_BUILD=true
 51            local_arch=true
 52            local_only=true
 53            ;;
 54        i) local_install=true;;
 55        h)
 56           help_info
 57           exit 0
 58           ;;
 59    esac
 60done
 61
 62shift $((OPTIND-1))
 63
 64if [[ $# -gt 0 ]]; then
 65    if [ "$1" ]; then
 66        bundle_name=$1
 67    fi
 68fi
 69
 70# Get release channel
 71pushd crates/zed
 72channel=$(<RELEASE_CHANNEL)
 73export ZED_RELEASE_CHANNEL="${channel}"
 74popd
 75
 76export ZED_BUNDLE=true
 77
 78cargo_bundle_version=$(cargo -q bundle --help 2>&1 | head -n 1 || echo "")
 79if [ "$cargo_bundle_version" != "cargo-bundle v0.6.1-zed" ]; then
 80    cargo install cargo-bundle --git https://github.com/zed-industries/cargo-bundle.git --branch zed-deploy
 81fi
 82
 83# Deal with versions of macOS that don't include libstdc++ headers
 84export CXXFLAGS="-stdlib=libc++"
 85
 86version_info=$(rustc --version --verbose)
 87host_line=$(echo "$version_info" | grep host)
 88local_target_triple=${host_line#*: }
 89
 90# Generate the licenses first, so they can be baked into the binaries
 91script/generate-licenses
 92
 93if [ "$local_arch" = true ]; then
 94    echo "Building for local target only."
 95    cargo build ${build_flag} --package zed --package cli --package remote_server
 96else
 97    rustup target add aarch64-apple-darwin
 98    rustup target add x86_64-apple-darwin
 99
100    echo "Compiling zed binaries"
101    cargo build ${build_flag} --package zed --package cli --target aarch64-apple-darwin --target x86_64-apple-darwin
102    # Build remote_server in separate invocation to prevent feature unification from other crates
103    # from influencing dynamic libraries required by it.
104    cargo build ${build_flag} --package remote_server     --target aarch64-apple-darwin --target x86_64-apple-darwin
105fi
106
107echo "Creating application bundle"
108pushd crates/zed
109cp Cargo.toml Cargo.toml.backup
110sed \
111    -i.backup \
112    "s/package.metadata.bundle-${channel}/package.metadata.bundle/" \
113    Cargo.toml
114
115if [ "$local_arch" = true ]; then
116    app_path=$(cargo bundle ${build_flag} --select-workspace-root | xargs)
117    cp -R target/${target_dir}/cli "${app_path}/Contents/MacOS/"
118else
119    app_path_x64=$(cargo bundle ${build_flag} --target x86_64-apple-darwin --select-workspace-root | xargs)
120    app_path_aarch64=$(cargo bundle ${build_flag} --target aarch64-apple-darwin --select-workspace-root | xargs)
121    app_path=$app_path_x64
122    prepare_binaries "aarch64-apple-darwin" "$app_path_aarch64"
123    prepare_binaries "x86_64-apple-darwin" "$app_path_x64"
124fi
125
126mv Cargo.toml.backup Cargo.toml
127popd
128echo "Built ${app_path}"
129
130function upload_debug_info() {
131    architecture=$1
132    if [[ -n "${SENTRY_AUTH_TOKEN:-}" ]]; then
133        echo "Uploading zed debug symbols to sentry..."
134        # note: this uploads the unstripped binary which is needed because it contains
135        # .eh_frame data for stack unwinding. see https://github.com/getsentry/symbolic/issues/783
136        sentry-cli debug-files upload --include-sources --wait -p zed -o zed-dev \
137            "target/${architecture}/${target_dir}/zed" \
138            "target/${architecture}/${target_dir}/remote_server" \
139            "target/${architecture}/${target_dir}/zed.dwarf"
140    else
141        echo "missing SENTRY_AUTH_TOKEN. skipping sentry upload."
142    fi
143}
144
145if command -v sentry-cli >/dev/null 2>&1; then
146    upload_debug_info "aarch64-apple-darwin"
147    upload_debug_info "x86_64-apple-darwin"
148else
149    echo "sentry-cli not found. skipping sentry upload."
150    echo "install with: 'curl -sL https://sentry.io/get-cli | bash'"
151fi