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