bundle

  1#!/bin/bash
  2
  3set -e
  4
  5build_flag="--release"
  6target_dir="release"
  7open_result=false
  8local_arch=false
  9local_only=false
 10overwrite_local_app=false
 11bundle_name=""
 12
 13# This must match the team in the provsiioning profile.
 14APPLE_NOTORIZATION_TEAM="MQ55VZLNZQ"
 15
 16# Function for displaying help info
 17help_info() {
 18  echo "
 19Usage: ${0##*/} [options] [bundle_name]
 20Build the application bundle.
 21
 22Options:
 23  -d    Compile in debug mode
 24  -l    Compile for local architecture and copy bundle to /Applications, implies -d.
 25  -o    Open the resulting DMG or the app itself in local mode.
 26  -f    Overwrite the local app bundle if it exists.
 27  -h    Display this help and exit.
 28  "
 29}
 30
 31# If -o option is specified, the folder of the resulting dmg will be opened in finder
 32# If -d is specified, Zed will be compiled in debug mode and the application's path printed
 33# If -od or -do is specified Zed will be bundled in debug and the application will be run.
 34while getopts 'dlfoh' 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            local_arch=true
 43            target_dir="debug"
 44            ;;
 45        l)
 46            export CARGO_INCREMENTAL=true
 47            export CARGO_BUNDLE_SKIP_BUILD=true
 48            build_flag=""
 49            local_arch=true
 50            local_only=true
 51            target_dir="debug"
 52            ;;
 53        f) overwrite_local_app=true;;
 54        h)
 55           help_info
 56           exit 0
 57           ;;
 58    esac
 59done
 60
 61shift $((OPTIND-1))
 62
 63if [ "$1" ]; then
 64    bundle_name=$1
 65fi
 66
 67export ZED_BUNDLE=true
 68export MACOSX_DEPLOYMENT_TARGET=10.15.7
 69
 70cargo_bundle_version=$(cargo -q bundle --help 2>&1 | head -n 1 || echo "")
 71if [ "$cargo_bundle_version" != "cargo-bundle v0.6.0-zed" ]; then
 72    cargo install cargo-bundle --git https://github.com/zed-industries/cargo-bundle.git --branch zed-deploy
 73fi
 74
 75rustup target add wasm32-wasi
 76
 77# Deal with versions of macOS that don't include libstdc++ headers
 78export CXXFLAGS="-stdlib=libc++"
 79
 80version_info=$(rustc --version --verbose)
 81host_line=$(echo "$version_info" | grep host)
 82local_target_triple=${host_line#*: }
 83
 84if [ "$local_arch" = true ]; then
 85    echo "Building for local target only."
 86    cargo build ${build_flag} --package zed
 87    cargo build ${build_flag} --package cli
 88else
 89    echo "Compiling zed binary for aarch64-apple-darwin"
 90    cargo build ${build_flag} --package zed --target aarch64-apple-darwin
 91    echo "Compiling zed binary for x86_64-apple-darwin"
 92    cargo build ${build_flag} --package zed --target x86_64-apple-darwin
 93    echo "Compiling cli binary for aarch64-apple-darwin"
 94    cargo build ${build_flag} --package cli --target aarch64-apple-darwin
 95    echo "Compiling cli binary for x86_64-apple-darwin"
 96    cargo build ${build_flag} --package cli --target x86_64-apple-darwin
 97fi
 98
 99echo "Creating application bundle"
100pushd crates/zed
101channel=$(<RELEASE_CHANNEL)
102cp Cargo.toml Cargo.toml.backup
103sed \
104    -i .backup \
105    "s/package.metadata.bundle-${channel}/package.metadata.bundle/" \
106    Cargo.toml
107
108if [ "$local_arch" = true ]; then
109    app_path=$(cargo bundle ${build_flag} --select-workspace-root | xargs)
110else
111    app_path=$(cargo bundle ${build_flag} --target x86_64-apple-darwin --select-workspace-root | xargs)
112fi
113
114mv Cargo.toml.backup Cargo.toml
115popd
116echo "Bundled ${app_path}"
117
118if [ "$local_arch" = false ]; then
119    echo "Creating fat binaries"
120    lipo \
121        -create \
122        target/{x86_64-apple-darwin,aarch64-apple-darwin}/${target_dir}/Zed \
123        -output \
124        "${app_path}/Contents/MacOS/zed"
125    lipo \
126        -create \
127        target/{x86_64-apple-darwin,aarch64-apple-darwin}/${target_dir}/cli \
128        -output \
129        "${app_path}/Contents/MacOS/cli"
130fi
131
132echo "Copying WebRTC.framework into the frameworks folder"
133mkdir "${app_path}/Contents/Frameworks"
134if [ "$local_arch" = false ]; then
135    cp -R target/${local_target_triple}/${target_dir}/WebRTC.framework "${app_path}/Contents/Frameworks/"
136else
137    cp -R target/${target_dir}/WebRTC.framework "${app_path}/Contents/Frameworks/"
138fi
139
140cp crates/zed/contents/$channel/embedded.provisionprofile "${app_path}/Contents/"
141
142if [[ -n $MACOS_CERTIFICATE && -n $MACOS_CERTIFICATE_PASSWORD && -n $APPLE_NOTARIZATION_USERNAME && -n $APPLE_NOTARIZATION_PASSWORD ]]; then
143    echo "Signing bundle with Apple-issued certificate"
144    security create-keychain -p "$MACOS_CERTIFICATE_PASSWORD" zed.keychain || echo ""
145    security default-keychain -s zed.keychain
146    security unlock-keychain -p "$MACOS_CERTIFICATE_PASSWORD" zed.keychain
147    echo "$MACOS_CERTIFICATE" | base64 --decode > /tmp/zed-certificate.p12
148    security import /tmp/zed-certificate.p12 -k zed.keychain -P "$MACOS_CERTIFICATE_PASSWORD" -T /usr/bin/codesign
149    rm /tmp/zed-certificate.p12
150    security set-key-partition-list -S apple-tool:,apple:,codesign: -s -k "$MACOS_CERTIFICATE_PASSWORD" zed.keychain
151
152    # sequence of codesign commands modeled after this example: https://developer.apple.com/forums/thread/701514
153    /usr/bin/codesign --deep --force --timestamp --sign "Zed Industries, Inc." "${app_path}/Contents/Frameworks/WebRTC.framework" -v
154    /usr/bin/codesign --deep --force --timestamp --options runtime --sign "Zed Industries, Inc." "${app_path}/Contents/MacOS/cli" -v
155    /usr/bin/codesign --deep --force --timestamp --options runtime --entitlements crates/zed/resources/zed.entitlements --sign "Zed Industries, Inc." "${app_path}/Contents/MacOS/zed" -v
156    /usr/bin/codesign --force --timestamp --options runtime --entitlements crates/zed/resources/zed.entitlements --sign "Zed Industries, Inc." "${app_path}" -v
157
158    security default-keychain -s login.keychain
159else
160    echo "One or more of the following variables are missing: MACOS_CERTIFICATE, MACOS_CERTIFICATE_PASSWORD, APPLE_NOTARIZATION_USERNAME, APPLE_NOTARIZATION_PASSWORD"
161    if [[ "$local_only" = false ]]; then
162        echo "To create a self-signed local build use ./scripts/build.sh -ldf"
163        exit 1
164    fi
165
166    echo "====== WARNING ======"
167    echo "This bundle is being signed without all entitlements, some features (e.g. universal links) will not work"
168    echo "====== WARNING ======"
169
170    # NOTE: if you need to test universal links you have a few paths forward:
171    # - create a PR and tag it with the `run-build-dmg` label, and download the .dmg file from there.
172    # - get a signing key for the MQ55VZLNZQ team from Nathan.
173    # - create your own signing key, and update references to MQ55VZLNZQ to your own team ID
174    # then comment out this line.
175    cat crates/zed/resources/zed.entitlements | sed '/com.apple.developer.associated-domains/,+1d' > "${app_path}/Contents/Resources/zed.entitlements"
176
177    codesign --force --deep --entitlements "${app_path}/Contents/Resources/zed.entitlements" --sign ${MACOS_SIGNING_KEY:- -} "${app_path}" -v
178fi
179
180if [[ "$target_dir" = "debug" && "$local_only" = false ]]; then
181    if [ "$open_result" = true ]; then
182        open "$app_path"
183    else
184        echo "Created application bundle:"
185        echo "$app_path"
186    fi
187    exit 0
188fi
189
190if [ "$local_only" = true ]; then
191    # If bundle_name is not set or empty, use the basename of $app_path
192    if [ -z "$bundle_name" ]; then
193        bundle_name=$(basename "$app_path")
194    else
195        # If bundle_name doesn't end in .app, append it
196        if [[ "$bundle_name" != *.app ]]; then
197            bundle_name="$bundle_name.app"
198        fi
199    fi
200
201    if [ "$overwrite_local_app" = true ]; then
202        rm -rf "/Applications/$bundle_name"
203    fi
204    mv "$app_path" "/Applications/$bundle_name"
205
206    if [ "$open_result" = true ]; then
207        open "/Applications/$bundle_name"
208    else
209        echo "Installed application bundle:"
210        echo "/Applications/$bundle_name"
211    fi
212else
213    echo "Creating DMG"
214    dmg_target_directory="target/${target_dir}"
215    dmg_source_directory="${dmg_target_directory}/dmg"
216    dmg_file_path="${dmg_target_directory}/Zed.dmg"
217
218    rm -rf ${dmg_source_directory}
219    mkdir -p ${dmg_source_directory}
220    mv "${app_path}" "${dmg_source_directory}"
221
222    ln -s /Applications ${dmg_source_directory}
223    hdiutil create -volname Zed -srcfolder "${dmg_source_directory}" -ov -format UDZO "${dmg_file_path}"
224    # If someone runs this bundle script locally, a symlink will be placed in `dmg_source_directory`.
225    # This symlink causes CPU issues with Zed if the Zed codebase is the project being worked on, so we simply remove it for now.
226    rm ${dmg_source_directory}/Applications
227
228    echo "Adding license agreement to DMG"
229    npm install --global dmg-license minimist
230    dmg-license script/eula/eula.json "${dmg_file_path}"
231
232    if [[ -n $MACOS_CERTIFICATE && -n $MACOS_CERTIFICATE_PASSWORD && -n $APPLE_NOTARIZATION_USERNAME && -n $APPLE_NOTARIZATION_PASSWORD ]]; then
233        echo "Notarizing DMG with Apple"
234        "$(xcode-select -p)/usr/bin/notarytool" submit --wait --apple-id "$APPLE_NOTARIZATION_USERNAME" --password "$APPLE_NOTARIZATION_PASSWORD" --team-id "$APPLE_NOTORIZATION_TEAM" "${dmg_file_path}"
235    fi
236
237    if [ "$open_result" = true ]; then
238        open $dmg_target_directory
239    fi
240fi