1#!/usr/bin/env bash
2
3set -euxo pipefail
4
5# Function for displaying help info
6help_info() {
7 echo "
8Usage: ${0##*/} [options]
9Build a release .tar.gz for Linux.
10
11Options:
12 -h Display this help and exit.
13 "
14}
15
16while getopts 'h' flag
17do
18 case "${flag}" in
19 h)
20 help_info
21 exit 0
22 ;;
23 esac
24done
25
26export ZED_BUNDLE=true
27
28channel=$(<crates/zed/RELEASE_CHANNEL)
29target_dir="${CARGO_TARGET_DIR:-target}"
30
31version="$(script/get-crate-version zed)"
32# Set RELEASE_VERSION so it's compiled into GPUI and it knows about the version.
33export RELEASE_VERSION="${version}"
34
35commit=$(git rev-parse HEAD | cut -c 1-7)
36
37version_info=$(rustc --version --verbose)
38host_line=$(echo "$version_info" | grep host)
39target_triple=${host_line#*: }
40musl_triple=${target_triple%-gnu}-musl
41remote_server_triple=${REMOTE_SERVER_TARGET:-"${musl_triple}"}
42rustup_installed=false
43if command -v rustup >/dev/null 2>&1; then
44 rustup_installed=true
45fi
46
47# Generate the licenses first, so they can be baked into the binaries
48script/generate-licenses
49
50if "$rustup_installed"; then
51 rustup target add "$remote_server_triple"
52fi
53
54# Build binary in release mode
55export RUSTFLAGS="${RUSTFLAGS:-} -C link-args=-Wl,--disable-new-dtags,-rpath,\$ORIGIN/../lib"
56cargo build --release --target "${target_triple}" --package zed --package cli
57# Build remote_server in separate invocation to prevent feature unification from other crates
58# from influencing dynamic libraries required by it.
59if [[ "$remote_server_triple" == "$musl_triple" ]]; then
60 export RUSTFLAGS="${RUSTFLAGS:-} -C target-feature=+crt-static"
61fi
62cargo build --release --target "${remote_server_triple}" --package remote_server
63
64# Strip the binary of all debug symbols
65# Later, we probably want to do something like this: https://github.com/GabrielMajeri/separate-symbols
66strip --strip-debug "${target_dir}/${target_triple}/release/zed"
67strip --strip-debug "${target_dir}/${target_triple}/release/cli"
68strip --strip-debug "${target_dir}/${remote_server_triple}/release/remote_server"
69
70
71# Ensure that remote_server does not depend on libssl nor libcrypto, as we got rid of these deps.
72! ldd "${target_dir}/${remote_server_triple}/release/remote_server" | grep -q 'libcrypto\|libssl'
73
74suffix=""
75if [ "$channel" != "stable" ]; then
76 suffix="-$channel"
77fi
78
79# Move everything that should end up in the final package
80# into a temp directory.
81temp_dir=$(mktemp -d)
82zed_dir="${temp_dir}/zed$suffix.app"
83
84# Binary
85mkdir -p "${zed_dir}/bin" "${zed_dir}/libexec"
86cp "${target_dir}/${target_triple}/release/zed" "${zed_dir}/libexec/zed-editor"
87cp "${target_dir}/${target_triple}/release/cli" "${zed_dir}/bin/zed"
88
89# Libs
90find_libs() {
91 ldd ${target_dir}/${target_triple}/release/zed |\
92 cut -d' ' -f3 |\
93 grep -v '\<\(libstdc++.so\|libc.so\|libgcc_s.so\|libm.so\|libpthread.so\|libdl.so\)'
94}
95
96mkdir -p "${zed_dir}/lib"
97rm -rf "${zed_dir}/lib/*"
98cp $(find_libs) "${zed_dir}/lib"
99
100# Icons
101mkdir -p "${zed_dir}/share/icons/hicolor/512x512/apps"
102cp "crates/zed/resources/app-icon$suffix.png" "${zed_dir}/share/icons/hicolor/512x512/apps/zed.png"
103mkdir -p "${zed_dir}/share/icons/hicolor/1024x1024/apps"
104cp "crates/zed/resources/app-icon$suffix@2x.png" "${zed_dir}/share/icons/hicolor/1024x1024/apps/zed.png"
105
106# .desktop
107export DO_STARTUP_NOTIFY="true"
108export APP_CLI="zed"
109export APP_ICON="zed"
110export APP_ARGS="%U"
111if [[ "$channel" == "preview" ]]; then
112 export APP_NAME="Zed Preview"
113elif [[ "$channel" == "nightly" ]]; then
114 export APP_NAME="Zed Nightly"
115elif [[ "$channel" == "dev" ]]; then
116 export APP_NAME="Zed Devel"
117else
118 export APP_NAME="Zed"
119fi
120
121mkdir -p "${zed_dir}/share/applications"
122envsubst < "crates/zed/resources/zed.desktop.in" > "${zed_dir}/share/applications/zed$suffix.desktop"
123
124# Copy generated licenses so they'll end up in archive too
125cp "assets/licenses.md" "${zed_dir}/licenses.md"
126
127# Create archive out of everything that's in the temp directory
128arch=$(uname -m)
129target="linux-${arch}"
130if [[ "$channel" == "dev" ]]; then
131 archive="zed-${commit}-${target}.tar.gz"
132else
133 archive="zed-${target}.tar.gz"
134fi
135
136rm -rf "${archive}"
137remove_match="zed(-[a-zA-Z0-9]+)?-linux-$(uname -m)\.tar\.gz"
138ls "${target_dir}/release" | grep -E ${remove_match} | xargs -d "\n" -I {} rm -f "${target_dir}/release/{}" || true
139tar -czvf "${target_dir}/release/$archive" -C ${temp_dir} "zed$suffix.app"
140
141gzip --stdout --best "${target_dir}/${remote_server_triple}/release/remote_server" > "${target_dir}/zed-remote-server-linux-${arch}.gz"