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