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="${MOLD_VERSION:-${1:-}}"
16if [ "$(uname -s)" != "Linux" ]; then
17    echo "Error: This script is intended for Linux systems only."
18    exit 1
19elif [ -z "$MOLD_VERSION" ]; then
20    echo "Usage: $0 2.34.0"
21    exit 1
22elif [ -e /usr/local/bin/mold ]; then
23    echo "Warning: existing mold found at /usr/local/bin/mold. Skipping installation."
24    exit 0
25fi
26
27if [ "$(whoami)" = root ]; then SUDO=; else SUDO="$(command -v sudo || command -v doas || true)"; fi
28
29MOLD_REPO="${MOLD_REPO:-https://github.com/rui314/mold}"
30MOLD_URL="${MOLD_URL:-$MOLD_REPO}/releases/download/v$MOLD_VERSION/mold-$MOLD_VERSION-$(uname -m)-linux.tar.gz"
31
32echo "Downloading from $MOLD_URL"
33curl -fsSL --output - "$MOLD_URL" \
34    | $SUDO tar -C /usr/local --strip-components=1 --no-overwrite-dir -xzf -
35
36# Note this binary depends on the system libatomic.so.1 which is usually
37# provided as a dependency of gcc so it should be available on most systems.
38
39cat <<EOF
40Mold is installed to /usr/local/bin/mold
41
42To make it your default, add or merge these lines into your ~/.cargo/config.toml:
43
44[target.'cfg(target_os = "linux")']
45linker = "clang"
46rustflags = ["-C", "link-arg=-fuse-ld=mold"]
47EOF