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