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