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