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