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
41
42# Generate the licenses first, so they can be baked into the binaries
43script/generate-licenses
44rustup target add "$musl_triple"
45
46# Build binary in release mode
47export RUSTFLAGS="${RUSTFLAGS:-} -C link-args=-Wl,--disable-new-dtags,-rpath,\$ORIGIN/../lib"
48cargo build --release --target "${target_triple}" --package zed --package cli
49# Build remote_server in separate invocation to prevent feature unification from other crates
50# from influencing dynamic libraries required by it.
51cargo build --release --target "${musl_triple}" --package remote_server
52
53# Strip the binary of all debug symbols
54# Later, we probably want to do something like this: https://github.com/GabrielMajeri/separate-symbols
55strip --strip-debug "${target_dir}/${target_triple}/release/zed"
56strip --strip-debug "${target_dir}/${target_triple}/release/cli"
57strip --strip-debug "${target_dir}/${musl_triple}/release/remote_server"
58
59
60# Ensure that remote_server does not depend on libssl nor libcrypto, as we got rid of these deps.
61! ldd "${target_dir}/${musl_triple}/release/remote_server" | grep -q 'libcrypto\|libssl'
62
63suffix=""
64if [ "$channel" != "stable" ]; then
65 suffix="-$channel"
66fi
67
68# Move everything that should end up in the final package
69# into a temp directory.
70temp_dir=$(mktemp -d)
71zed_dir="${temp_dir}/zed$suffix.app"
72
73# Binary
74mkdir -p "${zed_dir}/bin" "${zed_dir}/libexec"
75cp "${target_dir}/${target_triple}/release/zed" "${zed_dir}/libexec/zed-editor"
76cp "${target_dir}/${target_triple}/release/cli" "${zed_dir}/bin/zed"
77
78# Libs
79find_libs() {
80 ldd ${target_dir}/${target_triple}/release/zed |\
81 cut -d' ' -f3 |\
82 grep -v '\<\(libstdc++.so\|libc.so\|libgcc_s.so\|libm.so\|libpthread.so\|libdl.so\)'
83}
84
85mkdir -p "${zed_dir}/lib"
86rm -rf "${zed_dir}/lib/*"
87cp $(find_libs) "${zed_dir}/lib"
88
89# Icons
90mkdir -p "${zed_dir}/share/icons/hicolor/512x512/apps"
91cp "crates/zed/resources/app-icon$suffix.png" "${zed_dir}/share/icons/hicolor/512x512/apps/zed.png"
92mkdir -p "${zed_dir}/share/icons/hicolor/1024x1024/apps"
93cp "crates/zed/resources/app-icon$suffix@2x.png" "${zed_dir}/share/icons/hicolor/1024x1024/apps/zed.png"
94
95# .desktop
96export DO_STARTUP_NOTIFY="true"
97export APP_CLI="zed"
98export APP_ICON="zed"
99export APP_ARGS="%U"
100if [[ "$channel" == "preview" ]]; then
101 export APP_NAME="Zed Preview"
102elif [[ "$channel" == "nightly" ]]; then
103 export APP_NAME="Zed Nightly"
104elif [[ "$channel" == "dev" ]]; then
105 export APP_NAME="Zed Devel"
106else
107 export APP_NAME="Zed"
108fi
109
110mkdir -p "${zed_dir}/share/applications"
111envsubst < "crates/zed/resources/zed.desktop.in" > "${zed_dir}/share/applications/zed$suffix.desktop"
112
113# Copy generated licenses so they'll end up in archive too
114cp "assets/licenses.md" "${zed_dir}/licenses.md"
115
116# Create archive out of everything that's in the temp directory
117arch=$(uname -m)
118target="linux-${arch}"
119if [[ "$channel" == "dev" ]]; then
120 archive="zed-${commit}-${target}.tar.gz"
121else
122 archive="zed-${target}.tar.gz"
123fi
124
125rm -rf "${archive}"
126remove_match="zed(-[a-zA-Z0-9]+)?-linux-$(uname -m)\.tar\.gz"
127ls "${target_dir}/release" | grep -E ${remove_match} | xargs -d "\n" -I {} rm -f "${target_dir}/release/{}" || true
128tar -czvf "${target_dir}/release/$archive" -C ${temp_dir} "zed$suffix.app"
129
130gzip --stdout --best "${target_dir}/${musl_triple}/release/remote_server" > "${target_dir}/zed-remote-server-linux-${arch}.gz"