bundle-linux

  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        for attempt in 1 2 3; do
 96            echo "Attempting sentry upload (attempt $attempt/3)..."
 97            if sentry-cli debug-files upload --include-sources --wait -p zed -o zed-dev \
 98                "${target_dir}/${target_triple}"/release/zed \
 99                "${target_dir}/${remote_server_triple}"/release/remote_server; then
100                echo "Sentry upload successful on attempt $attempt"
101                break
102            else
103                echo "Sentry upload failed on attempt $attempt"
104                if [ $attempt -eq 3 ]; then
105                    echo "All sentry upload attempts failed"
106                fi
107            fi
108        done
109    else
110        echo "missing SENTRY_AUTH_TOKEN. skipping sentry upload."
111    fi
112fi
113
114# Strip debug symbols and save them for upload to DigitalOcean
115objcopy --strip-debug "${target_dir}/${target_triple}/release/zed"
116objcopy --strip-debug "${target_dir}/${target_triple}/release/cli"
117objcopy --strip-debug "${target_dir}/${remote_server_triple}/release/remote_server"
118
119# Ensure that remote_server does not depend on libssl nor libcrypto, as we got rid of these deps.
120if ldd "${target_dir}/${remote_server_triple}/release/remote_server" | grep -q 'libcrypto\|libssl'; then
121    if [[ "$remote_server_triple" == *-musl ]]; then
122        echo "Error: remote_server still depends on libssl or libcrypto" && exit 1
123    else
124        echo "Info: Using non-musl remote-server build."
125    fi
126fi
127
128suffix=""
129if [ "$channel" != "stable" ]; then
130  suffix="-$channel"
131fi
132
133# Move everything that should end up in the final package
134# into a temp directory.
135temp_dir=$(mktemp -d)
136zed_dir="${temp_dir}/zed$suffix.app"
137
138# Binary
139mkdir -p "${zed_dir}/bin" "${zed_dir}/libexec"
140cp "${target_dir}/${target_triple}/release/zed" "${zed_dir}/libexec/zed-editor"
141cp "${target_dir}/${target_triple}/release/cli" "${zed_dir}/bin/zed"
142
143# Libs
144find_libs() {
145    ldd ${target_dir}/${target_triple}/release/zed |\
146        cut -d' ' -f3 |\
147        grep -v '\<\(libstdc++.so\|libc.so\|libgcc_s.so\|libm.so\|libpthread.so\|libdl.so\|libasound.so\)'
148}
149
150mkdir -p "${zed_dir}/lib"
151rm -rf "${zed_dir}/lib/*"
152cp $(find_libs) "${zed_dir}/lib"
153
154# Icons
155mkdir -p "${zed_dir}/share/icons/hicolor/512x512/apps"
156cp "crates/zed/resources/app-icon$suffix.png" "${zed_dir}/share/icons/hicolor/512x512/apps/zed.png"
157mkdir -p "${zed_dir}/share/icons/hicolor/1024x1024/apps"
158cp "crates/zed/resources/app-icon$suffix@2x.png" "${zed_dir}/share/icons/hicolor/1024x1024/apps/zed.png"
159
160# .desktop
161export DO_STARTUP_NOTIFY="true"
162export APP_CLI="zed"
163export APP_ICON="zed"
164export APP_ARGS="%U"
165if [[ "$channel" == "preview" ]]; then
166  export APP_NAME="Zed Preview"
167elif [[ "$channel" == "nightly" ]]; then
168  export APP_NAME="Zed Nightly"
169elif [[ "$channel" == "dev" ]]; then
170  export APP_NAME="Zed Devel"
171else
172  export APP_NAME="Zed"
173fi
174
175mkdir -p "${zed_dir}/share/applications"
176envsubst < "crates/zed/resources/zed.desktop.in" > "${zed_dir}/share/applications/zed$suffix.desktop"
177chmod +x "${zed_dir}/share/applications/zed$suffix.desktop"
178
179# Copy generated licenses so they'll end up in archive too
180cp "assets/licenses.md" "${zed_dir}/licenses.md"
181
182# Create archive out of everything that's in the temp directory
183arch=$(uname -m)
184archive="zed-linux-${arch}.tar.gz"
185
186rm -rf "${archive}"
187remove_match="zed(-[a-zA-Z0-9]+)?-linux-$(uname -m)\.tar\.gz"
188ls "${target_dir}/release" | grep -E ${remove_match} | xargs -d "\n" -I {} rm -f "${target_dir}/release/{}" || true
189tar -czvf "${target_dir}/release/$archive" -C ${temp_dir} "zed$suffix.app"
190
191gzip -f --stdout --best "${target_dir}/${remote_server_triple}/release/remote_server" > "${target_dir}/zed-remote-server-linux-${arch}.gz"