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