| 
 | 1 | +#!/bin/bash  | 
 | 2 | + | 
 | 3 | +# Script to build and install zstd on Ubuntu 24, with -fPIC flag.  | 
 | 4 | +# The default installation of zstd on Ubuntu 24 does not have -fPIC flag  | 
 | 5 | +# enabled, which is required for building DPC++ in shared libraries mode.  | 
 | 6 | + | 
 | 7 | +# OR on Rocky Linux 8.10 (used for nightly release builds). There is no static  | 
 | 8 | +# library (libzstd.a) in available packages, therefore it is necessary to build  | 
 | 9 | +# it from source.  | 
 | 10 | + | 
 | 11 | +# Function to check OS  | 
 | 12 | +check_os() {  | 
 | 13 | +    local expected_name="$1"  | 
 | 14 | +    local expected_version="$2"  | 
 | 15 | +    . /etc/os-release  | 
 | 16 | +    if [[ "$NAME" == "$expected_name" && "$VERSION_ID" == "$expected_version" ]]; then  | 
 | 17 | +        return 0  | 
 | 18 | +    else  | 
 | 19 | +        return 1  | 
 | 20 | +    fi  | 
 | 21 | +}  | 
 | 22 | + | 
 | 23 | +# Function to install packages with or without sudo  | 
 | 24 | +install_packages() {  | 
 | 25 | +    if [ "$USE_SUDO" = true ]; then  | 
 | 26 | +        sudo apt-get update  | 
 | 27 | +        sudo apt-get install -y build-essential wget  | 
 | 28 | +    else  | 
 | 29 | +        apt-get update  | 
 | 30 | +        apt-get install -y build-essential wget  | 
 | 31 | +    fi  | 
 | 32 | +}  | 
 | 33 | + | 
 | 34 | +# Function to uninstall libzstd-dev if installed  | 
 | 35 | +uninstall_libzstd_dev() {  | 
 | 36 | +    if dpkg -l | grep -q libzstd-dev; then  | 
 | 37 | +        if [ "$USE_SUDO" = true ]; then  | 
 | 38 | +            sudo apt-get remove -y libzstd-dev  | 
 | 39 | +        else  | 
 | 40 | +            apt-get remove -y libzstd-dev  | 
 | 41 | +        fi  | 
 | 42 | +    fi  | 
 | 43 | +}  | 
 | 44 | + | 
 | 45 | +# Function to build a shared library by linking zstd static lib.  | 
 | 46 | +# This is used to verify that zstd is built correctly, with -fPIC flag.  | 
 | 47 | +build_test_program() {  | 
 | 48 | +    cat <<EOF > test_zstd.c  | 
 | 49 | +      #include <zstd.h>  | 
 | 50 | +      int main() {  | 
 | 51 | +        ZSTD_CCtx* cctx = ZSTD_createCCtx();  | 
 | 52 | +        ZSTD_freeCCtx(cctx);  | 
 | 53 | +        return 0;  | 
 | 54 | +      }  | 
 | 55 | +EOF  | 
 | 56 | + | 
 | 57 | +    # Try to use zstd's static library with -fPIC  | 
 | 58 | +    gcc test_zstd.c -lzstd -fPIC -shared  | 
 | 59 | +    if [ $? -ne 0 ]; then  | 
 | 60 | +        echo "zstd installation verification failed."  | 
 | 61 | +    else  | 
 | 62 | +        echo "zstd installation verification passed."  | 
 | 63 | +    fi  | 
 | 64 | + | 
 | 65 | +    # There won't be a.out file if verification failed.  | 
 | 66 | +    rm test_zstd.c a.out || true  | 
 | 67 | +}  | 
 | 68 | + | 
 | 69 | +# Check the OS  | 
 | 70 | +if ! check_os "Ubuntu" "24.04" && ! check_os "Rocky Linux" "8.10"; then  | 
 | 71 | +    echo "Warning: This script has only been tested with Ubuntu 24.04 and Rocky Linux 8.10."  | 
 | 72 | +fi  | 
 | 73 | + | 
 | 74 | +# Set USE_SUDO to true or false based on your preference  | 
 | 75 | +USE_SUDO=true  | 
 | 76 | + | 
 | 77 | +# Install necessary build tools & uninstall libzstd-dev package if installed  | 
 | 78 | +if check_os "Ubuntu" "24.04"; then  | 
 | 79 | +    install_packages  | 
 | 80 | +    uninstall_libzstd_dev  | 
 | 81 | +fi  | 
 | 82 | + | 
 | 83 | +# Define the version and URL for zstd  | 
 | 84 | +ZSTD_VERSION="1.5.7"  | 
 | 85 | +ZSTD_URL="https://github.com/facebook/zstd/releases/download/v$ZSTD_VERSION/zstd-$ZSTD_VERSION.tar.gz"  | 
 | 86 | + | 
 | 87 | +# Create a directory for the source code  | 
 | 88 | +mkdir -p zstd_build  | 
 | 89 | +cd zstd_build  | 
 | 90 | + | 
 | 91 | +# Download and extract zstd source code  | 
 | 92 | +wget $ZSTD_URL  | 
 | 93 | +tar -xzf zstd-$ZSTD_VERSION.tar.gz  | 
 | 94 | +cd zstd-$ZSTD_VERSION  | 
 | 95 | + | 
 | 96 | +# Build zstd with -fPIC flag.  | 
 | 97 | +CFLAGS="-fPIC" CXXFLAGS="-fPIC" make  | 
 | 98 | +if [ $? -ne 0 ]; then  | 
 | 99 | +    echo "Error: make failed."  | 
 | 100 | +    exit 1  | 
 | 101 | +fi  | 
 | 102 | + | 
 | 103 | +# Install zstd.  | 
 | 104 | +if [ "$USE_SUDO" = true ]; then  | 
 | 105 | +    sudo make install  | 
 | 106 | +else  | 
 | 107 | +    make install  | 
 | 108 | +fi  | 
 | 109 | +if [ $? -ne 0 ]; then  | 
 | 110 | +    echo "Error: make install failed."  | 
 | 111 | +    exit 1  | 
 | 112 | +fi  | 
 | 113 | + | 
 | 114 | +# Verify zstd installation.  | 
 | 115 | +build_test_program  | 
 | 116 | + | 
 | 117 | +# Clean up  | 
 | 118 | +rm -rf zstd_build  | 
0 commit comments