1#!/usr/bin/env bash
  2
  3set -euxo pipefail
  4source script/lib/blob-store.sh
  5
  6# Function for displaying help info
  7help_info() {
  8  echo "
  9Usage: ${0##*/} [options]
 10Build a release .tar.gz for Linux.
 11
 12Options:
 13  -h, --help     Display this help and exit.
 14  --flatpak      Set ZED_BUNDLE_TYPE=flatpak so that this can be included in system info
 15  "
 16}
 17
 18# Parse all arguments manually
 19while [[ $# -gt 0 ]]; do
 20    case $1 in
 21        -h|--help)
 22            help_info
 23            exit 0
 24            ;;
 25        --flatpak)
 26            export ZED_BUNDLE_TYPE=flatpak
 27            shift
 28            ;;
 29        --)
 30            shift
 31            break
 32            ;;
 33        -*)
 34            echo "Unknown option: $1" >&2
 35            help_info
 36            exit 1
 37            ;;
 38        *)
 39            echo "Error: Unexpected argument: $1" >&2
 40            help_info
 41            exit 1
 42            ;;
 43    esac
 44done
 45
 46export ZED_BUNDLE=true
 47
 48channel=$(<crates/zed/RELEASE_CHANNEL)
 49target_dir="${CARGO_TARGET_DIR:-target}"
 50
 51version="$(script/get-crate-version zed)"
 52# Set RELEASE_VERSION so it's compiled into GPUI and it knows about the version.
 53export RELEASE_VERSION="${version}"
 54
 55commit=$(git rev-parse HEAD | cut -c 1-7)
 56
 57version_info=$(rustc --version --verbose)
 58host_line=$(echo "$version_info" | grep host)
 59target_triple=${host_line#*: }
 60musl_triple=${target_triple%-gnu}-musl
 61remote_server_triple=${REMOTE_SERVER_TARGET:-"${musl_triple}"}
 62rustup_installed=false
 63if command -v rustup >/dev/null 2>&1; then
 64    rustup_installed=true
 65fi
 66
 67# Generate the licenses first, so they can be baked into the binaries
 68script/generate-licenses
 69
 70if "$rustup_installed"; then
 71    rustup target add "$remote_server_triple"
 72fi
 73
 74export CC=$(which clang)
 75
 76# Build binary in release mode
 77export RUSTFLAGS="${RUSTFLAGS:-} -C link-args=-Wl,--disable-new-dtags,-rpath,\$ORIGIN/../lib"
 78cargo build --release --target "${target_triple}" --package zed --package cli
 79# Build remote_server in separate invocation to prevent feature unification from other crates
 80# from influencing dynamic libraries required by it.
 81if [[ "$remote_server_triple" == "$musl_triple" ]]; then
 82    export RUSTFLAGS="${RUSTFLAGS:-} -C target-feature=+crt-static"
 83fi
 84cargo build --release --target "${remote_server_triple}" --package remote_server
 85
 86# Upload debug info to sentry.io
 87if ! command -v sentry-cli >/dev/null 2>&1; then
 88    echo "sentry-cli not found. skipping sentry upload."
 89    echo "install with: 'curl -sL https://sentry.io/get-cli | bash'"
 90else
 91    if [[ -n "${SENTRY_AUTH_TOKEN:-}" ]]; then
 92        echo "Uploading zed debug symbols to sentry..."
 93        # note: this uploads the unstripped binary which is needed because it contains
 94        # .eh_frame data for stack unwinding. see https://github.com/getsentry/symbolic/issues/783
 95        sentry-cli debug-files upload --include-sources --wait -p zed -o zed-dev \
 96            "${target_dir}/${target_triple}"/release/zed \
 97            "${target_dir}/${remote_server_triple}"/release/remote_server
 98    else
 99        echo "missing SENTRY_AUTH_TOKEN. skipping sentry upload."
100    fi
101fi
102
103# Strip debug symbols and save them for upload to DigitalOcean
104objcopy --strip-debug "${target_dir}/${target_triple}/release/zed"
105objcopy --strip-debug "${target_dir}/${target_triple}/release/cli"
106objcopy --strip-debug "${target_dir}/${remote_server_triple}/release/remote_server"
107
108# Ensure that remote_server does not depend on libssl nor libcrypto, as we got rid of these deps.
109if ldd "${target_dir}/${remote_server_triple}/release/remote_server" | grep -q 'libcrypto\|libssl'; then
110    if [[ "$remote_server_triple" == *-musl ]]; then
111        echo "Error: remote_server still depends on libssl or libcrypto" && exit 1
112    else
113        echo "Info: Using non-musl remote-server build."
114    fi
115fi
116
117suffix=""
118if [ "$channel" != "stable" ]; then
119  suffix="-$channel"
120fi
121
122# Move everything that should end up in the final package
123# into a temp directory.
124temp_dir=$(mktemp -d)
125zed_dir="${temp_dir}/zed$suffix.app"
126
127# Binary
128mkdir -p "${zed_dir}/bin" "${zed_dir}/libexec"
129cp "${target_dir}/${target_triple}/release/zed" "${zed_dir}/libexec/zed-editor"
130cp "${target_dir}/${target_triple}/release/cli" "${zed_dir}/bin/zed"
131
132# Libs
133find_libs() {
134    ldd ${target_dir}/${target_triple}/release/zed |\
135        cut -d' ' -f3 |\
136        grep -v '\<\(libstdc++.so\|libc.so\|libgcc_s.so\|libm.so\|libpthread.so\|libdl.so\|libasound.so\)'
137}
138
139mkdir -p "${zed_dir}/lib"
140rm -rf "${zed_dir}/lib/*"
141cp $(find_libs) "${zed_dir}/lib"
142
143# Icons
144mkdir -p "${zed_dir}/share/icons/hicolor/512x512/apps"
145cp "crates/zed/resources/app-icon$suffix.png" "${zed_dir}/share/icons/hicolor/512x512/apps/zed.png"
146mkdir -p "${zed_dir}/share/icons/hicolor/1024x1024/apps"
147cp "crates/zed/resources/app-icon$suffix@2x.png" "${zed_dir}/share/icons/hicolor/1024x1024/apps/zed.png"
148
149# .desktop
150export DO_STARTUP_NOTIFY="true"
151export APP_CLI="zed"
152export APP_ICON="zed"
153export APP_ARGS="%U"
154if [[ "$channel" == "preview" ]]; then
155  export APP_NAME="Zed Preview"
156elif [[ "$channel" == "nightly" ]]; then
157  export APP_NAME="Zed Nightly"
158elif [[ "$channel" == "dev" ]]; then
159  export APP_NAME="Zed Devel"
160else
161  export APP_NAME="Zed"
162fi
163
164mkdir -p "${zed_dir}/share/applications"
165envsubst < "crates/zed/resources/zed.desktop.in" > "${zed_dir}/share/applications/zed$suffix.desktop"
166chmod +x "${zed_dir}/share/applications/zed$suffix.desktop"
167
168# Copy generated licenses so they'll end up in archive too
169cp "assets/licenses.md" "${zed_dir}/licenses.md"
170
171# Create archive out of everything that's in the temp directory
172arch=$(uname -m)
173target="linux-${arch}"
174if  [[ "$channel" == "dev" ]]; then
175  archive="zed-${commit}-${target}.tar.gz"
176else
177  archive="zed-${target}.tar.gz"
178fi
179
180rm -rf "${archive}"
181remove_match="zed(-[a-zA-Z0-9]+)?-linux-$(uname -m)\.tar\.gz"
182ls "${target_dir}/release" | grep -E ${remove_match} | xargs -d "\n" -I {} rm -f "${target_dir}/release/{}" || true
183tar -czvf "${target_dir}/release/$archive" -C ${temp_dir} "zed$suffix.app"
184
185gzip -f --stdout --best "${target_dir}/${remote_server_triple}/release/remote_server" > "${target_dir}/zed-remote-server-linux-${arch}.gz"