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.
72if ldd "${target_dir}/${remote_server_triple}/release/remote_server" | grep -q 'libcrypto\|libssl'; then
73 echo "Error: remote_server still depends on libssl or libcrypto" && exit 1
74fi
75
76suffix=""
77if [ "$channel" != "stable" ]; then
78 suffix="-$channel"
79fi
80
81# Move everything that should end up in the final package
82# into a temp directory.
83temp_dir=$(mktemp -d)
84zed_dir="${temp_dir}/zed$suffix.app"
85
86# Binary
87mkdir -p "${zed_dir}/bin" "${zed_dir}/libexec"
88cp "${target_dir}/${target_triple}/release/zed" "${zed_dir}/libexec/zed-editor"
89cp "${target_dir}/${target_triple}/release/cli" "${zed_dir}/bin/zed"
90
91# Libs
92find_libs() {
93 ldd ${target_dir}/${target_triple}/release/zed |\
94 cut -d' ' -f3 |\
95 grep -v '\<\(libstdc++.so\|libc.so\|libgcc_s.so\|libm.so\|libpthread.so\|libdl.so\|libasound.so\)'
96}
97
98mkdir -p "${zed_dir}/lib"
99rm -rf "${zed_dir}/lib/*"
100cp $(find_libs) "${zed_dir}/lib"
101
102# Icons
103mkdir -p "${zed_dir}/share/icons/hicolor/512x512/apps"
104cp "crates/zed/resources/app-icon$suffix.png" "${zed_dir}/share/icons/hicolor/512x512/apps/zed.png"
105mkdir -p "${zed_dir}/share/icons/hicolor/1024x1024/apps"
106cp "crates/zed/resources/app-icon$suffix@2x.png" "${zed_dir}/share/icons/hicolor/1024x1024/apps/zed.png"
107
108# .desktop
109export DO_STARTUP_NOTIFY="true"
110export APP_CLI="zed"
111export APP_ICON="zed"
112export APP_ARGS="%U"
113if [[ "$channel" == "preview" ]]; then
114 export APP_NAME="Zed Preview"
115elif [[ "$channel" == "nightly" ]]; then
116 export APP_NAME="Zed Nightly"
117elif [[ "$channel" == "dev" ]]; then
118 export APP_NAME="Zed Devel"
119else
120 export APP_NAME="Zed"
121fi
122
123mkdir -p "${zed_dir}/share/applications"
124envsubst < "crates/zed/resources/zed.desktop.in" > "${zed_dir}/share/applications/zed$suffix.desktop"
125
126# Copy generated licenses so they'll end up in archive too
127cp "assets/licenses.md" "${zed_dir}/licenses.md"
128
129# Create archive out of everything that's in the temp directory
130arch=$(uname -m)
131target="linux-${arch}"
132if [[ "$channel" == "dev" ]]; then
133 archive="zed-${commit}-${target}.tar.gz"
134else
135 archive="zed-${target}.tar.gz"
136fi
137
138rm -rf "${archive}"
139remove_match="zed(-[a-zA-Z0-9]+)?-linux-$(uname -m)\.tar\.gz"
140ls "${target_dir}/release" | grep -E ${remove_match} | xargs -d "\n" -I {} rm -f "${target_dir}/release/{}" || true
141tar -czvf "${target_dir}/release/$archive" -C ${temp_dir} "zed$suffix.app"
142
143gzip --stdout --best "${target_dir}/${remote_server_triple}/release/remote_server" > "${target_dir}/zed-remote-server-linux-${arch}.gz"