zed/script/bundle-linux
Nitin Krishna Mucheli 9aa48e46e9
Fix musl linking failure in bundle-linux by setting CC for musl target (#61203)
Fixes https://github.com/zed-industries/zed/issues/24880 

# Objective
"Assisted/analysed by AI"
> The remote_server musl build failed with undefined references to
__isoc23_sscanf and __isoc23_strtol from aws-lc-sys because the default
C compiler used glibc headers while linking against musl libc. Setting
CC_<target>=musl-gcc ensures C dependencies compile with musl headers.

I have tested the fix, and it seems to have fixed the compilation issue
I faced on Fedora 44, without resorting to the [hack noted in
docs](https://zed.dev/docs/development/linux#installing-a-development-build).
Initially reported in this
[issue](https://github.com/zed-industries/zed/issues/24880).

I'd want someone with expertise in the domain to take a look at this
fix, as from what I understand, the process in the doc is a hacky
workaround.

## Solution

Setting CC_<target>=musl-gcc ensures C dependencies compile with musl
headers.

## Testing

- Did you test these changes? If so, how?

    <details><summary>Here's my steps to repro:</summary>
1. install [toolbox](https://github.com/containers/toolbox) (or
podman-toolbox)
(I used toolbox as it's easy to reuse the cloned repo, as the whole home
dir is mounted in the devcontainer)
Used the main branch - commit 5d6c88cdb7
    2. toolbox create -d fedora -r 44
    3. toolbox enter fedora-toolbox-44
    4. then run the `scripts/linux` script to install prereqs
5. try compiling remote-server using the commands in
`scripts/install-linux` (I just ran the script wholly)
    
    LDD version:
    ```
    ⬢[root@toolbox zed]# ldd --version
    ldd (GNU libc) 2.43
    ```
    
    GCC/MUSL versions:
    ```
    musl-gcc  : 1.2.5
    gcc : 16.1.1
    ```
    
    Error:
    ```
    error: linking with `cc` failed: exit status: 1
      |
= note: "cc" "-m64"
"<sysroot>/lib/rustlib/x86_64-unknown-linux-musl/lib/self-contained/rcrt1.o"
"<sysroot>/lib/rustlib/x86_64-unknown-linux-musl/lib/self-contained/crti.o"
"<sysroot>/lib/rustlib/x86_64-unknown-linux-musl/lib/self-contained/crtbeginS.o"
"<747 object files omitted>" "-Wl,--as-needed" "-Wl,-Bstatic"
"/root/zed/target/x86_64-unknown-linux-musl/release/deps/rustceHhjo8/{libzstd_sys-5c3eb6739555ecf4,libtree_sitter_json-ad90c8af7e8a3750,libtree_sitter-f91a92b688e12e56,libwasmtime-decd3377028506ce,libaws_lc_sys-a051f708b6dafc2a,libring-fcc1b43e3092bc93,libpsm-d967f50c6f2b67a4}.rlib"
"-lunwind" "-lc"
"<sysroot>/lib/rustlib/x86_64-unknown-linux-musl/lib/libcompiler_builtins-*.rlib"
"-L"
"/root/zed/target/x86_64-unknown-linux-musl/release/deps/rustceHhjo8/raw-dylibs"
"-Wl,-Bdynamic" "-Wl,--eh-frame-hdr" "-Wl,-z,noexecstack"
"-nostartfiles" "-L"
"/root/zed/target/x86_64-unknown-linux-musl/release/build/psm-dc603221f407eabc/out"
"-L"
"/root/zed/target/x86_64-unknown-linux-musl/release/build/tree-sitter-28f7b87c1db3fab9/out"
"-L"
"/root/zed/target/x86_64-unknown-linux-musl/release/build/wasmtime-f0bb0950e09a7352/out"
"-L"
"/root/zed/target/x86_64-unknown-linux-musl/release/build/aws-lc-sys-2a7ca8eafafe18fa/out"
"-L"
"/root/zed/target/x86_64-unknown-linux-musl/release/build/ring-d7857a166bac60a6/out"
"-L"
"/root/zed/target/x86_64-unknown-linux-musl/release/build/zstd-sys-0e04609af7157148/out"
"-L"
"/root/zed/target/x86_64-unknown-linux-musl/release/build/tree-sitter-json-b1e2eeaba7bbb519/out"
"-L"
"<sysroot>/lib/rustlib/x86_64-unknown-linux-musl/lib/self-contained"
"-L" "<sysroot>/lib/rustlib/x86_64-unknown-linux-musl/lib" "-o"
"/root/zed/target/x86_64-unknown-linux-musl/release/deps/remote_server-3782b255c59d4e14"
"-Wl,--gc-sections" "-static-pie" "-Wl,-z,relro,-z,now" "-Wl,-O1"
"-nodefaultlibs" "-Wl,--disable-new-dtags,-rpath,$ORIGIN/../lib"
"<sysroot>/lib/rustlib/x86_64-unknown-linux-musl/lib/self-contained/crtendS.o"
"<sysroot>/lib/rustlib/x86_64-unknown-linux-musl/lib/self-contained/crtn.o"
= note: some arguments are omitted. use `--verbose` to show all linker
arguments
= note: /usr/bin/ld.bfd:
/root/zed/target/x86_64-unknown-linux-musl/release/deps/rustceHhjo8/libaws_lc_sys-a051f708b6dafc2a.rlib(f8e4fd781484bd36-bcm.o):
in function `aws_lc_0_40_0_handle_cpu_env':

/aws-lc/crypto/fipsmodule/cpucap/cpu_intel.c:(.text.aws_lc_0_40_0_handle_cpu_env+0x63):
undefined reference to `__isoc23_sscanf'
/usr/bin/ld.bfd:
/root/zed/target/x86_64-unknown-linux-musl/release/deps/rustceHhjo8/libaws_lc_sys-a051f708b6dafc2a.rlib(f8e4fd781484bd36-bcm.o):
in function `pkey_rsa_ctrl_str':

/aws-lc/crypto/fipsmodule/evp/p_rsa.c:692:(.text.pkey_rsa_ctrl_str+0x222):
undefined reference to `__isoc23_strtol'
/usr/bin/ld.bfd:
/aws-lc/crypto/fipsmodule/evp/p_rsa.c:703:(.text.pkey_rsa_ctrl_str+0x261):
undefined reference to `__isoc23_strtol'
              collect2: error: ld returned 1 exit status
              
= note: some `extern` functions couldn't be found; some native libraries
may need to be installed or have their path specified
      = note: use the `-l` flag to specify native libraries to link
= note: use the `cargo:rustc-link-lib` directive to specify the native
libraries to link with Cargo (see
https://doc.rust-lang.org/cargo/reference/build-scripts.html#rustc-link-lib)
    
error: could not compile `remote_server` (bin "remote_server") due to 1
previous error
    ```
    </details>

- Are there any parts that need more testing?  -- Maybe
- How can other people (reviewers) test your changes? Is there anything
specific they need to know? -- Steps listed above
- If relevant, what platforms did you test these changes on, and are
there any important ones you can't test? -- Fedora 44

## Self-Review Checklist:

- [x] I've reviewed my own diff for quality, security, and reliability
- [x] Unsafe blocks (if any) have justifying comments
- [x] The content adheres to Zed's UI standards
([UX/UI](https://github.com/zed-industries/zed/blob/main/CONTRIBUTING.md#uiux-checklist)
and
[icon](https://github.com/zed-industries/zed/blob/main/crates/icons/README.md)
guidelines)
- [ ] Tests cover the new/changed behavior
- [ ] Performance impact has been considered and is acceptable

## Showcase

> This section is optional. If this PR does not include a visual change
or does not add a new user-facing feature, you can delete this section.

- Help others understand the result of this PR by showcasing your
awesome work!
- If this PR includes a visual change, consider adding a screenshot,
GIF, or video
- A before/after comparison is very useful for changes to existing
features!

While a showcase should aim to be brief and digestible, you can use a
toggleable section to save space on longer showcases:

<details>
  <summary>Click to view showcase</summary>

My super cool demos here

</details>

---

Release Notes:

- Fixed musl error when building remote server from source for the
default musl triple on Linux

Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
2026-07-22 09:37:55 +00:00

209 lines
7.3 KiB
Bash
Executable file

#!/usr/bin/env bash
set -euxo pipefail
source script/lib/blob-store.sh
# Function for displaying help info
help_info() {
echo "
Usage: ${0##*/} [options]
Build a release .tar.gz for Linux.
Options:
-h, --help Display this help and exit.
--flatpak Set ZED_BUNDLE_TYPE=flatpak so that this can be included in system info
"
}
# Parse all arguments manually
while [[ $# -gt 0 ]]; do
case $1 in
-h|--help)
help_info
exit 0
;;
--flatpak)
export ZED_BUNDLE_TYPE=flatpak
shift
;;
--)
shift
break
;;
-*)
echo "Unknown option: $1" >&2
help_info
exit 1
;;
*)
echo "Error: Unexpected argument: $1" >&2
help_info
exit 1
;;
esac
done
export ZED_BUNDLE=true
channel=$(<crates/zed/RELEASE_CHANNEL)
target_dir="${CARGO_TARGET_DIR:-target}"
version="$(script/get-crate-version zed)"
# Set RELEASE_VERSION so it's compiled into GPUI and it knows about the version.
export RELEASE_VERSION="${version}"
commit=$(git rev-parse HEAD | cut -c 1-7)
version_info=$(rustc --version --verbose)
host_line=$(echo "$version_info" | grep host)
target_triple=${host_line#*: }
musl_triple=${target_triple%-gnu}-musl
remote_server_triple=${REMOTE_SERVER_TARGET:-"${musl_triple}"}
rustup_installed=false
if command -v rustup >/dev/null 2>&1; then
rustup_installed=true
fi
# Generate the licenses first, so they can be baked into the binaries
script/generate-licenses
if "$rustup_installed"; then
rustup target add "$remote_server_triple"
fi
export CC=${CC:-$(which clang)}
# Build binary in release mode
# We need lld to link libwebrtc.a successfully on aarch64-linux.
# NOTE: Since RUSTFLAGS env var overrides all .cargo/config.toml rustflags
# (see https://github.com/rust-lang/cargo/issues/5376), the
# [target.aarch64-unknown-linux-gnu] section in config.toml has no effect here.
if [[ "$(uname -m)" == "aarch64" ]]; then
export RUSTFLAGS="${RUSTFLAGS:-} -C link-arg=-fuse-ld=lld -C link-args=-Wl,--disable-new-dtags,-rpath,\$ORIGIN/../lib"
else
export RUSTFLAGS="${RUSTFLAGS:-} -C link-args=-Wl,--disable-new-dtags,-rpath,\$ORIGIN/../lib"
fi
cargo build --release --target "${target_triple}" --package zed --package cli
# Build remote_server in separate invocation to prevent feature unification from other crates
# from influencing dynamic libraries required by it.
if [[ "$remote_server_triple" == "$musl_triple" ]]; then
export RUSTFLAGS="${RUSTFLAGS:-} -C target-feature=+crt-static"
musl_cc_var="CC_$(echo "$remote_server_triple" | tr '-' '_')"
export "$musl_cc_var"=musl-gcc
fi
cargo build --release --target "${remote_server_triple}" --package remote_server
# Upload debug info to sentry.io
if ! command -v sentry-cli >/dev/null 2>&1; then
echo "sentry-cli not found. skipping sentry upload."
echo "install with: 'curl -sL https://sentry.io/get-cli | bash'"
else
if [[ -n "${SENTRY_AUTH_TOKEN:-}" ]]; then
echo "Uploading zed debug symbols to sentry..."
# note: this uploads the unstripped binary which is needed because it contains
# .eh_frame data for stack unwinding. see https://github.com/getsentry/symbolic/issues/783
for attempt in 1 2 3; do
echo "Attempting sentry upload (attempt $attempt/3)..."
if sentry-cli debug-files upload --include-sources --wait -p zed -o zed-dev \
"${target_dir}/${target_triple}"/release/zed \
"${target_dir}/${remote_server_triple}"/release/remote_server; then
echo "Sentry upload successful on attempt $attempt"
break
else
echo "Sentry upload failed on attempt $attempt"
if [ $attempt -eq 3 ]; then
echo "All sentry upload attempts failed"
fi
fi
done
else
echo "missing SENTRY_AUTH_TOKEN. skipping sentry upload."
fi
fi
# Strip debug symbols and save them for upload to DigitalOcean.
# We use llvm-objcopy because GNU objcopy on older distros (e.g. Ubuntu 20.04)
# doesn't understand CREL sections produced by newer LLVM.
llvm-objcopy --strip-debug "${target_dir}/${target_triple}/release/zed"
llvm-objcopy --strip-debug "${target_dir}/${target_triple}/release/cli"
llvm-objcopy --strip-debug "${target_dir}/${remote_server_triple}/release/remote_server"
# Ensure that remote_server does not depend on libssl nor libcrypto, as we got rid of these deps.
if ldd "${target_dir}/${remote_server_triple}/release/remote_server" | grep -q 'libcrypto\|libssl'; then
if [[ "$remote_server_triple" == *-musl ]]; then
echo "Error: remote_server still depends on libssl or libcrypto" && exit 1
else
echo "Info: Using non-musl remote-server build."
fi
fi
suffix=""
if [ "$channel" != "stable" ]; then
suffix="-$channel"
fi
# Move everything that should end up in the final package
# into a temp directory.
temp_dir=$(mktemp -d)
zed_dir="${temp_dir}/zed$suffix.app"
# Binary
mkdir -p "${zed_dir}/bin" "${zed_dir}/libexec"
cp "${target_dir}/${target_triple}/release/zed" "${zed_dir}/libexec/zed-editor"
cp "${target_dir}/${target_triple}/release/cli" "${zed_dir}/bin/zed"
# Libs
# Bundle libstdc++ so older supported systems can run binaries built with our
# toolchain even when their system libstdc++.so.6 lacks required GLIBCXX symbols.
find_libs() {
ldd ${target_dir}/${target_triple}/release/zed |\
cut -d' ' -f3 |\
grep -v '\<\(libc.so\|libgcc_s.so\|libm.so\|libpthread.so\|libdl.so\|libasound.so\)'
}
mkdir -p "${zed_dir}/lib"
rm -rf "${zed_dir}/lib/*"
cp $(find_libs) "${zed_dir}/lib"
# Icons
mkdir -p "${zed_dir}/share/icons/hicolor/512x512/apps"
cp "crates/zed/resources/app-icon$suffix.png" "${zed_dir}/share/icons/hicolor/512x512/apps/zed.png"
mkdir -p "${zed_dir}/share/icons/hicolor/1024x1024/apps"
cp "crates/zed/resources/app-icon$suffix@2x.png" "${zed_dir}/share/icons/hicolor/1024x1024/apps/zed.png"
# .desktop
export DO_STARTUP_NOTIFY="true"
export APP_CLI="zed"
export APP_ICON="zed"
export APP_ARGS="%U"
if [[ "$channel" == "preview" ]]; then
export APP_NAME="Zed Preview"
APP_ID="dev.zed.Zed-Preview"
elif [[ "$channel" == "nightly" ]]; then
export APP_NAME="Zed Nightly"
APP_ID="dev.zed.Zed-Nightly"
elif [[ "$channel" == "dev" ]]; then
export APP_NAME="Zed Devel"
APP_ID="dev.zed.Zed-Dev"
else
export APP_NAME="Zed"
APP_ID="dev.zed.Zed"
fi
mkdir -p "${zed_dir}/share/applications"
envsubst < "crates/zed/resources/zed.desktop.in" > "${zed_dir}/share/applications/$APP_ID.desktop"
chmod +x "${zed_dir}/share/applications/$APP_ID.desktop"
# Copy generated licenses so they'll end up in archive too
cp "assets/licenses.md" "${zed_dir}/licenses.md"
# Create archive out of everything that's in the temp directory
arch=$(uname -m)
archive="zed-linux-${arch}.tar.gz"
rm -rf "${archive}"
remove_match="zed(-[a-zA-Z0-9]+)?-linux-$(uname -m)\.tar\.gz"
ls "${target_dir}/release" | grep -E ${remove_match} | xargs -d "\n" -I {} rm -f "${target_dir}/release/{}" || true
tar -czvf "${target_dir}/release/$archive" -C ${temp_dir} "zed$suffix.app"
gzip -f --stdout --best "${target_dir}/${remote_server_triple}/release/remote_server" > "${target_dir}/zed-remote-server-linux-${arch}.gz"