From 6ef7b80fd31f1c68ffead7ba6ac27d496e4b4188 Mon Sep 17 00:00:00 2001 From: Ian Duncan <76043277+dr8co@users.noreply.github.com> Date: Wed, 5 Jun 2024 21:00:59 +0300 Subject: [PATCH] Refactor build scripts --- scripts/build-functions.sh | 63 ++++++++++++++++++++++++++++ scripts/build.sh | 86 +++++++++----------------------------- 2 files changed, 82 insertions(+), 67 deletions(-) create mode 100755 scripts/build-functions.sh diff --git a/scripts/build-functions.sh b/scripts/build-functions.sh new file mode 100755 index 0000000..1233b8e --- /dev/null +++ b/scripts/build-functions.sh @@ -0,0 +1,63 @@ +#!/usr/bin/env bash + +set -e + +# This script is used to build the project on the Ubuntu Jammy (22.04) distribution. +# It is not intended to be used on other distributions, and must be run from the project root. + +PARALLELISM_LEVEL=4 + +function check_root() { + # Root access is required to install the dependencies. + if [ "$EUID" -ne 0 ]; then + echo "Please run as root." + abort + fi +} + +function check_dependencies() { + for cmd in wget add-apt-repository; do + if ! command -v $cmd &>/dev/null; then + echo "$cmd could not be found" + exit + fi + done +} + +function install_dependencies() { +# wget -qO- https://apt.llvm.org/llvm-snapshot.gpg.key | tee /etc/apt/trusted.gpg.d/apt.llvm.org.asc +# add-apt-repository -y "deb http://apt.llvm.org/jammy/ llvm-toolchain-jammy-18 main" +# add-apt-repository -y ppa:ubuntu-toolchain-r/ppa + apt update + export NEEDRESTART_SUSPEND=1 + apt install -y unzip gcc-14 g++-14 clang-18 lldb-18 lld-18 libc++-18-dev libc++abi-18-dev libllvmlibc-18-dev clang-tools-18 libgcrypt20-dev openssl libreadline8 libreadline-dev libsodium23 libsodium-dev + + # Install CMake 3.29.3 + if dpkg -s "cmake" >/dev/null 2>&1; then + apt remove -y --purge --auto-remove cmake + fi + + wget -qO- "https://github.com/Kitware/CMake/releases/download/v3.29.3/cmake-3.29.3-linux-x86_64.tar.gz" | tar --strip-components=1 -xz -C /usr/local + + # Install Ninja 1.12 + if dpkg -s "ninja-build" >/dev/null 2>&1; then + apt remove -y --purge --auto-remove ninja-build + fi + + wget -q "https://github.com/ninja-build/ninja/releases/download/v1.12.1/ninja-linux.zip" + unzip ninja-linux.zip -d /usr/local/bin +} + +function build_blake3() { + ./install-blake3.sh clang-18 +} + +function configure_cmake() { + cmake -B build -DCMAKE_C_COMPILER=clang-18 -DCMAKE_CXX_COMPILER=clang++-18 -DCMAKE_BUILD_TYPE=Debug -G Ninja +} + +function build_project() { + cmake --build build --config Debug -j "$PARALLELISM_LEVEL" +} + +trap "echo 'An unexpected error occurred. Program aborted.'" ERR diff --git a/scripts/build.sh b/scripts/build.sh index a2b0e3d..df8169a 100755 --- a/scripts/build.sh +++ b/scripts/build.sh @@ -1,76 +1,28 @@ -#!/bin/env bash +#!/usr/bin/env bash -set -e +# Run from this directory +cd "${0%/*}" || abort -# This script is used to build the project on the Ubuntu Jammy (22.04) distribution. -# It is not intended to be used on other distributions, and must be run from the project root. +# Include the build functions +. ./build-functions.sh -PARALLELISM_LEVEL=4 +# Root access is required to install the dependencies. +check_root -function check_root() { - # Root access is required to install the dependencies. - if [ "$EUID" -ne 0 ]; then - echo "Please run as root." - abort - fi -} +# Check for required commands +check_dependencies -function check_dependencies() { - for cmd in wget add-apt-repository; do - if ! command -v $cmd &>/dev/null; then - echo "$cmd could not be found" - exit - fi - done -} +# Install dependencies +install_dependencies -function install_dependencies() { -# wget -qO- https://apt.llvm.org/llvm-snapshot.gpg.key | tee /etc/apt/trusted.gpg.d/apt.llvm.org.asc -# add-apt-repository -y "deb http://apt.llvm.org/jammy/ llvm-toolchain-jammy-18 main" -# add-apt-repository -y ppa:ubuntu-toolchain-r/ppa - apt update - export NEEDRESTART_SUSPEND=1 - apt install -y unzip gcc-14 g++-14 clang-18 lldb-18 lld-18 libc++-18-dev libc++abi-18-dev libllvmlibc-18-dev clang-tools-18 libgcrypt20-dev openssl libreadline8 libreadline-dev libsodium23 libsodium-dev +echo "Ninja: $(ninja --version), CMake: $(cmake --version)" - # Install CMake 3.29.3 - if dpkg -s "cmake" >/dev/null 2>&1; then - apt remove -y --purge --auto-remove cmake - fi +# Build and install BLAKE3 +build_blake3 - wget -qO- "https://github.com/Kitware/CMake/releases/download/v3.29.3/cmake-3.29.3-linux-x86_64.tar.gz" | tar --strip-components=1 -xz -C /usr/local +# Configure CMake +cd .. || abort +configure_cmake - # Install Ninja 1.12 - if dpkg -s "ninja-build" >/dev/null 2>&1; then - apt remove -y --purge --auto-remove ninja-build - fi - - wget -q "https://github.com/ninja-build/ninja/releases/download/v1.12.1/ninja-linux.zip" - unzip ninja-linux.zip -d /usr/local/bin -} - -function build_blake3() { - ./install-blake3.sh clang-18 -} - -function configure_cmake() { - cmake -B build -DCMAKE_C_COMPILER=clang-18 -DCMAKE_CXX_COMPILER=clang++-18 -DCMAKE_BUILD_TYPE=Debug -G Ninja -} - -function build_project() { - cmake --build build --config Debug -j "$PARALLELISM_LEVEL" -} - -main() { - trap "echo 'An unexpected error occurred. Program aborted.'" ERR - check_root - check_dependencies - cd "${0%/*}" || abort - install_dependencies - echo "Ninja: $(ninja --version), CMake: $(cmake --version)" - build_blake3 - cd .. || abort - configure_cmake - build_project -} - -main +# Build the project +build_project \ No newline at end of file