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