1#!/usr/bin/env bash
 2
 3# Install `mold` official binaries from GitHub Releases.
 4#
 5# Adapted from the official rui314/setup-mold@v1 action to:
 6# * use environment variables instead of action inputs
 7# * remove make-default support
 8# * use curl instead of wget
 9# * support doas for sudo
10# * support redhat systems
11# See: https://github.com/rui314/setup-mold/blob/main/action.yml
12
13set -euo pipefail
14
15MOLD_VERSION="2.34.0"
16
17if [ "$(uname -s)" != "Linux" ]; then
18    echo "Error: This script is intended for Linux systems only."
19    exit 1
20elif [ -e /usr/local/bin/mold ]; then
21    echo "Warning: existing mold found at /usr/local/bin/mold. Skipping installation."
22    exit 0
23fi
24
25if [ "$(whoami)" = root ]; then SUDO=; else SUDO="$(command -v sudo || command -v doas || true)"; fi
26
27MOLD_REPO="${MOLD_REPO:-https://github.com/rui314/mold}"
28MOLD_URL="${MOLD_URL:-$MOLD_REPO}/releases/download/v$MOLD_VERSION/mold-$MOLD_VERSION-$(uname -m)-linux.tar.gz"
29
30echo "Downloading from $MOLD_URL"
31curl -fsSL --output - "$MOLD_URL" \
32    | $SUDO tar -C /usr/local --strip-components=1 --no-overwrite-dir -xzf -
33
34# Note this binary depends on the system libatomic.so.1 which is usually
35# provided as a dependency of gcc so it should be available on most systems.
36
37cat <<EOF
38Mold is installed to /usr/local/bin/mold
39
40To make it your default, add or merge these lines into your ~/.cargo/config.toml:
41
42[target.'cfg(target_os = "linux")']
43linker = "clang"
44rustflags = ["-C", "link-arg=-fuse-ld=mold"]
45EOF