#!/bin/bash

# Slackware build script for llvm-opt

# Copyright 2025 Ruoh-Shoei LIN
# All rights reserved.
#
# Redistribution and use of this script, with or without modification, is
# permitted provided that the following conditions are met:
#
# 1. Redistributions of this script must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
#
#  THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR IMPLIED
#  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
#  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO
#  EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
#  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
#  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
#  OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
#  WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
#  OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
#  ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

cd $(dirname $0) ; CWD=$(pwd)

PRGNAM=llvm-opt
SRCNAM=llvm
VERSION=${VERSION:-20.1.8}
BUILD=${BUILD:-2}
TAG=${TAG:-_SBo}
PKGTYPE=${PKGTYPE:-tgz}

if [ -z "$ARCH" ]; then
  case "$( uname -m )" in
    i?86) ARCH=i586 ;;
    arm*) ARCH=arm ;;
       *) ARCH=$( uname -m ) ;;
  esac
fi

if [ ! -z "${PRINT_PACKAGE_NAME}" ]; then
  echo "$PRGNAM-$VERSION-$ARCH-$BUILD$TAG.$PKGTYPE"
  exit 0
fi

TMP=${TMP:-/tmp/SBo}
PKG=$TMP/package-$PRGNAM
OUTPUT=${OUTPUT:-/tmp}

if [ "$ARCH" = "i586" ]; then
  SLKCFLAGS="-O2 -march=i686 -mtune=i686"
  LIBDIRSUFFIX=""
  LINKER_FLAG=
  export LDFLAGS="$LDFLAGS --rtlib=compiler-rt -latomic"
elif [ "$ARCH" = "i686" ]; then
  SLKCFLAGS="-O2 -march=i686 -mtune=i686"
  LIBDIRSUFFIX=""
  LINKER_FLAG=
  export LDFLAGS="$LDFLAGS --rtlib=compiler-rt -latomic"
elif [ "$ARCH" = "x86_64" ]; then
  SLKCFLAGS="-O2 -fPIC"
  LIBDIRSUFFIX="64"
  LINKER_FLAG=-DLLVM_USE_LINKER=lld
else
  SLKCFLAGS="-O2"
  LIBDIRSUFFIX=""
  LINKER_FLAG=
fi

set -e

rm -rf $PKG
mkdir -p $TMP $PKG $OUTPUT
cd $TMP
rm -rf $SRCNAM-project-$VERSION.src
tar xvf $CWD/$SRCNAM-project-$VERSION.src.tar.xz
cd $SRCNAM-project-$VERSION.src
chown -R root:root .
find -L . \
 \( -perm 777 -o -perm 775 -o -perm 750 -o -perm 711 -o -perm 555 \
  -o -perm 511 \) -exec chmod 755 '{}' \+ -o \
 \( -perm 666 -o -perm 664 -o -perm 640 -o -perm 600 -o -perm 444 \
  -o -perm 440 -o -perm 400 \) -exec chmod 644 '{}' \+

if [ "$ARCH" = "i586" -o "$ARCH" = "i686" ]; then
    LLVM_ENABLE_PROJECTS=${LLVM_ENABLE_PROJECTS:-"clang;clang-tools-extra;compiler-rt;lld;lldb;llvm"}
else
    LLVM_ENABLE_PROJECTS=${LLVM_ENABLE_PROJECTS:-"clang;clang-tools-extra;compiler-rt;lld;lldb;llvm"}
fi

LLVM_ENABLE_RUNTIMES=${LLVM_ENABLE_RUNTIMES:-"libcxx;libcxxabi"}

patch -Np1 -i $CWD/clang.toolchains.32-bit.triple.patch
patch -Np1 -i $CWD/llvm.no.clang_tidy_checks.patch

mkdir -p build
cd build
  cmake -GNinja \
    -DCMAKE_BUILD_TYPE=Release \
    -DCMAKE_CXX_COMPILER="clang++" \
    -DCMAKE_CXX_FLAGS:STRING="$SLKCFLAGS" \
    -DCMAKE_C_COMPILER="clang" \
    -DCMAKE_C_FLAGS:STRING="$SLKCFLAGS" \
    -DCMAKE_INSTALL_DOCDIR="doc/$PRGNAM-$VERSION" \
    -DCMAKE_INSTALL_MANDIR=man \
    -DCMAKE_INSTALL_PREFIX="/opt/$PRGNAM" \
    -DCLANG_DEFAULT_PIE_ON_LINUX=ON \
    -DCLANG_LINK_CLANG_DYLIB=ON \
    -DCOMPILER_RT_BUILD_LIBFUZZER=OFF \
    -DLLVM_BINUTILS_INCDIR=/usr/include \
    -DLLVM_BUILD_DOCS=OFF \
    -DLLVM_BUILD_LLVM_DYLIB=ON \
    -DLLVM_ENABLE_ASSERTIONS=OFF \
    -DLLVM_ENABLE_FFI=ON \
    -DLLVM_ENABLE_PER_TARGET_RUNTIME_DIR=NO \
    -DLLVM_ENABLE_PROJECTS="$LLVM_ENABLE_PROJECTS" \
    -DLLVM_ENABLE_RTTI=ON \
    -DLLVM_ENABLE_RUNTIMES="$LLVM_ENABLE_RUNTIMES" \
    -DLLVM_ENABLE_ZLIB=ON \
    -DLLVM_INCLUDE_BENCHMARKS=OFF \
    -DLLVM_INCLUDE_EXAMPLES=OFF \
    -DLLVM_INCLUDE_TESTS=OFF \
    -DLLVM_INSTALL_UTILS=ON \
    -DLLVM_LINK_LLVM_DYLIB=ON \
    -DLLVM_TARGETS_TO_BUILD="host;AMDGPU" \
    $LINKER_FLAG \
    -DMAN_INSTALL_DIR=/opt/$SRCNAM/man \
    -DLIB_SUFFIX="${LIBDIRSUFFIX}" \
    -DLLVM_LIBDIR_SUFFIX="${LIBDIRSUFFIX}" \
    -DLIBCXX_LIBDIR_SUFFIX="${LIBDIRSUFFIX}" \
    -DLIBCXXABI_LIBDIR_SUFFIX="${LIBDIRSUFFIX}" \
    -DLIBCXXABI_USE_LLVM_UNWINDER=OFF \
    -DLIBCXX_ENABLE_ABI_LINKER_SCRIPT=ON \
    -DLIBCXX_INCLUDE_BENCHMARKS=OFF \
    -Wno-dev \
    ../$SRCNAM
  ninja
  DESTDIR=$PKG ninja install
cd ..

install -v -dm755 "$PKG/usr/lib${LIBDIRSUFFIX}"
cd "$PKG/usr/lib${LIBDIRSUFFIX}"
  ln -sfv ../../opt/$PRGNAM/lib${LIBDIRSUFFIX}/libLLVM.so.${VERSION%.*}
  ln -sfv ../../opt/$PRGNAM/lib${LIBDIRSUFFIX}/libclang.so.${VERSION}
  ln -sfv ../../opt/$PRGNAM/lib${LIBDIRSUFFIX}/libclang-cpp.so.${VERSION%.*}
  ln -sfv libLLVM.so.${VERSION%.*} libLLVM-${VERSION%.*.*}.so
  ln -sfv libclang.so.${VERSION} libclang.so.${VERSION%.*}
cd -

find $PKG -print0 | xargs -0 file | grep -e "executable" -e "shared object" | grep ELF \
  | cut -f 1 -d : | xargs strip --strip-unneeded 2> /dev/null || true

mkdir -p $PKG/usr/doc/$PRGNAM-$VERSION
cp -a \
  LICENSE.TXT README.md \
  $PKG/usr/doc/$PRGNAM-$VERSION
cat $CWD/$PRGNAM.SlackBuild > $PKG/usr/doc/$PRGNAM-$VERSION/$PRGNAM.SlackBuild

mkdir -p $PKG/install
cat $CWD/slack-desc > $PKG/install/slack-desc

cd $PKG
/sbin/makepkg -l y -c n $OUTPUT/$PRGNAM-$VERSION-$ARCH-$BUILD$TAG.$PKGTYPE
