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