Skip to content

Instantly share code, notes, and snippets.

@sebsto
Last active November 21, 2024 02:55
Show Gist options
  • Save sebsto/6409ed9dbf4c9dd1488e049165f751aa to your computer and use it in GitHub Desktop.
Save sebsto/6409ed9dbf4c9dd1488e049165f751aa to your computer and use it in GitHub Desktop.
Build Swift 6.x on Amazon Linux 2023
#!/bin/bash
## Install and Remove conflicting dependencies
sudo dnf install python3-pip -y
sudo dnf remove python3-requests python-urllib3 -y
## Install docker (used by swift-installer-script project)
sudo dnf install docker -y
sudo usermod -a -G docker ec2-user
sudo newgrp docker
sudo systemctl enable docker.service
sudo systemctl start docker.service
# Install docker compose v2
DOCKER_CONFIG=${DOCKER_CONFIG:-$HOME/.docker}
mkdir -p $DOCKER_CONFIG/cli-plugins
curl -SL https://github.com/docker/compose/releases/download/v2.24.5/docker-compose-linux-x86_64 -o $DOCKER_CONFIG/cli-plugins/docker-compose
chmod +x $DOCKER_CONFIG/cli-plugins/docker-compose
# Invoke it with : docker compose xxxx
## Install Swift build dependencies
sudo dnf -y install \
clang \
cmake \
curl-devel \
gcc-c++ \
git \
glibc-static \
libbsd-devel \
libedit-devel \
libicu-devel \
libuuid-devel \
libxml2-devel \
ncurses-devel \
ninja-build \
python3-pexpect \
pkgconfig \
procps-ng \
python \
python3-devel \
python3-six \
python3-psutil \
python3-pkgconfig \
rsync \
sqlite-devel \
swig \
tzdata \
unzip \
uuid-devel \
wget \
which \
zip \
tar
## Install Swift 5.10 to bootstrap compilation of Swift 6.0
ARCH_NAME="$(rpm --eval '%{_arch}')"
case "${ARCH_NAME##*-}" in
'x86_64')
OS_ARCH_SUFFIX=''
;;
'aarch64')
OS_ARCH_SUFFIX='-aarch64'
;;
*) echo >&2 "error: unsupported architecture: '$ARCH_NAME'"; exit 1 ;;
esac
SWIFT_PLATFORM=amazonlinux2
SWIFT_BRANCH=swift-5.10-release
SWIFT_VERSION=swift-5.10-RELEASE
SWIFT_WEBROOT=https://download.swift.org
SWIFT_WEBDIR="$SWIFT_WEBROOT/$SWIFT_BRANCH/$(echo $SWIFT_PLATFORM | tr -d .)$OS_ARCH_SUFFIX"
SWIFT_BIN_URL="$SWIFT_WEBDIR/$SWIFT_VERSION/$SWIFT_VERSION-$SWIFT_PLATFORM$OS_ARCH_SUFFIX.tar.gz"
wget $SWIFT_BIN_URL
tar xfvz $SWIFT_VERSION-$SWIFT_PLATFORM$OS_ARCH_SUFFIX.tar.gz
export PATH=/home/ec2-user/$SWIFT_VERSION-$SWIFT_PLATFORM$OS_ARCH_SUFFIX/usr/bin/:$PATH
## Install LD GOLD
## This is used by Swift 5.10 and not available by default on Amazon Linux 2023
## Swift 6 selects the ldd linker by default on Amazon Linux 2023,
## so we just need ld.gold to build Swift 6, not at runtime
## https://github.com/swiftlang/swift/pull/72049
sudo dnf install gmp-devel mpfr-devel texinfo bison git gcc-c++ -y
mkdir ld.gold && cd ld.gold
git clone --depth 1 git://sourceware.org/git/binutils-gdb.git binutils
mkdir build && cd build
../binutils/configure --enable-gold --enable-plugins --disable-werror
make all-gold
cd gold
make all-am
cd ..
cp gold/ld-new /usr/bin/ld.gold
cd ~
/usr/bin/ld.gold -v
mkdir swift-project && cd swift-project
git clone https://github.com/apple/swift.git swift
cd swift
./utils/update-checkout --clone
## Create an EBS snapshot or an AMI at this point because the above takes ~30 minutes
## Build Instructions
## Specify the release you want to build
## IMPORTANT : to build a version >= 5.9, you must install Swift 5.8 or later and make sure `swift` is in the PATH
SWIFT_VERSION=6.0.0
./utils/update-checkout --scheme release/$SWIFT_VERSION
## Building
# simple build
# ./utils/build-script --release-debuginfo --skip-early-swift-driver
# or
# build and package
DIST_DIR=$HOME/dist
mkdir $DIST_DIR
./utils/build-script --preset=buildbot_linux,no_assertions,no_test install_destdir=$DIST_DIR installable_package=$DIST_DIR/swift-${SWIFT_VERSION}-amazonlinux2023.tar.gz
## Cleaning in between builds
rm -rf ../build
rm -rf $DIST_DIR
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment