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=${CC:-$(which clang)}
75
76# Build binary in release mode
77# We need lld to link libwebrtc.a successfully on aarch64-linux.
78# NOTE: Since RUSTFLAGS env var overrides all .cargo/config.toml rustflags
79# (see https://github.com/rust-lang/cargo/issues/5376), the
80# [target.aarch64-unknown-linux-gnu] section in config.toml has no effect here.
81if [[ "$(uname -m)" == "aarch64" ]]; then
82 export RUSTFLAGS="${RUSTFLAGS:-} -C link-arg=-fuse-ld=lld -C link-args=-Wl,--disable-new-dtags,-rpath,\$ORIGIN/../lib"
83else
84 export RUSTFLAGS="${RUSTFLAGS:-} -C link-args=-Wl,--disable-new-dtags,-rpath,\$ORIGIN/../lib"
85fi
86cargo build --release --target "${target_triple}" --package zed --package cli
87# Build remote_server in separate invocation to prevent feature unification from other crates
88# from influencing dynamic libraries required by it.
89if [[ "$remote_server_triple" == "$musl_triple" ]]; then
90 export RUSTFLAGS="${RUSTFLAGS:-} -C target-feature=+crt-static"
91fi
92cargo build --release --target "${remote_server_triple}" --package remote_server
93
94# Upload debug info to sentry.io
95if ! command -v sentry-cli >/dev/null 2>&1; then
96 echo "sentry-cli not found. skipping sentry upload."
97 echo "install with: 'curl -sL https://sentry.io/get-cli | bash'"
98else
99 if [[ -n "${SENTRY_AUTH_TOKEN:-}" ]]; then
100 echo "Uploading zed debug symbols to sentry..."
101 # note: this uploads the unstripped binary which is needed because it contains
102 # .eh_frame data for stack unwinding. see https://github.com/getsentry/symbolic/issues/783
103 for attempt in 1 2 3; do
104 echo "Attempting sentry upload (attempt $attempt/3)..."
105 if sentry-cli debug-files upload --include-sources --wait -p zed -o zed-dev \
106 "${target_dir}/${target_triple}"/release/zed \
107 "${target_dir}/${remote_server_triple}"/release/remote_server; then
108 echo "Sentry upload successful on attempt $attempt"
109 break
110 else
111 echo "Sentry upload failed on attempt $attempt"
112 if [ $attempt -eq 3 ]; then
113 echo "All sentry upload attempts failed"
114 fi
115 fi
116 done
117 else
118 echo "missing SENTRY_AUTH_TOKEN. skipping sentry upload."
119 fi
120fi
121
122# Strip debug symbols and save them for upload to DigitalOcean.
123# We use llvm-objcopy because GNU objcopy on older distros (e.g. Ubuntu 20.04)
124# doesn't understand CREL sections produced by newer LLVM.
125llvm-objcopy --strip-debug "${target_dir}/${target_triple}/release/zed"
126llvm-objcopy --strip-debug "${target_dir}/${target_triple}/release/cli"
127llvm-objcopy --strip-debug "${target_dir}/${remote_server_triple}/release/remote_server"
128
129# Ensure that remote_server does not depend on libssl nor libcrypto, as we got rid of these deps.
130if ldd "${target_dir}/${remote_server_triple}/release/remote_server" | grep -q 'libcrypto\|libssl'; then
131 if [[ "$remote_server_triple" == *-musl ]]; then
132 echo "Error: remote_server still depends on libssl or libcrypto" && exit 1
133 else
134 echo "Info: Using non-musl remote-server build."
135 fi
136fi
137
138suffix=""
139if [ "$channel" != "stable" ]; then
140 suffix="-$channel"
141fi
142
143# Move everything that should end up in the final package
144# into a temp directory.
145temp_dir=$(mktemp -d)
146zed_dir="${temp_dir}/zed$suffix.app"
147
148# Binary
149mkdir -p "${zed_dir}/bin" "${zed_dir}/libexec"
150cp "${target_dir}/${target_triple}/release/zed" "${zed_dir}/libexec/zed-editor"
151cp "${target_dir}/${target_triple}/release/cli" "${zed_dir}/bin/zed"
152
153# Libs
154find_libs() {
155 ldd ${target_dir}/${target_triple}/release/zed |\
156 cut -d' ' -f3 |\
157 grep -v '\<\(libstdc++.so\|libc.so\|libgcc_s.so\|libm.so\|libpthread.so\|libdl.so\|libasound.so\)'
158}
159
160mkdir -p "${zed_dir}/lib"
161rm -rf "${zed_dir}/lib/*"
162cp $(find_libs) "${zed_dir}/lib"
163
164# Icons
165mkdir -p "${zed_dir}/share/icons/hicolor/512x512/apps"
166cp "crates/zed/resources/app-icon$suffix.png" "${zed_dir}/share/icons/hicolor/512x512/apps/zed.png"
167mkdir -p "${zed_dir}/share/icons/hicolor/1024x1024/apps"
168cp "crates/zed/resources/app-icon$suffix@2x.png" "${zed_dir}/share/icons/hicolor/1024x1024/apps/zed.png"
169
170# .desktop
171export DO_STARTUP_NOTIFY="true"
172export APP_CLI="zed"
173export APP_ICON="zed"
174export APP_ARGS="%U"
175if [[ "$channel" == "preview" ]]; then
176 export APP_NAME="Zed Preview"
177 APP_ID="dev.zed.Zed-Preview"
178elif [[ "$channel" == "nightly" ]]; then
179 export APP_NAME="Zed Nightly"
180 APP_ID="dev.zed.Zed-Nightly"
181elif [[ "$channel" == "dev" ]]; then
182 export APP_NAME="Zed Devel"
183 APP_ID="dev.zed.Zed-Dev"
184else
185 export APP_NAME="Zed"
186 APP_ID="dev.zed.Zed"
187fi
188
189mkdir -p "${zed_dir}/share/applications"
190envsubst < "crates/zed/resources/zed.desktop.in" > "${zed_dir}/share/applications/$APP_ID.desktop"
191chmod +x "${zed_dir}/share/applications/$APP_ID.desktop"
192
193# Copy generated licenses so they'll end up in archive too
194cp "assets/licenses.md" "${zed_dir}/licenses.md"
195
196# Create archive out of everything that's in the temp directory
197arch=$(uname -m)
198archive="zed-linux-${arch}.tar.gz"
199
200rm -rf "${archive}"
201remove_match="zed(-[a-zA-Z0-9]+)?-linux-$(uname -m)\.tar\.gz"
202ls "${target_dir}/release" | grep -E ${remove_match} | xargs -d "\n" -I {} rm -f "${target_dir}/release/{}" || true
203tar -czvf "${target_dir}/release/$archive" -C ${temp_dir} "zed$suffix.app"
204
205gzip -f --stdout --best "${target_dir}/${remote_server_triple}/release/remote_server" > "${target_dir}/zed-remote-server-linux-${arch}.gz"