# Build recipe for llvm.
#
# Copyright (c) 2021-2023 Matias Fonzo, <selk@dragora.org>.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# Exit immediately on any error
set -e

program=llvm
version=15.0.7
release=1

# Define a category for the output of the package name
pkgcategory=devel

tarname=${program}-${version}.src.tar.xz
tarname_clang=clang-${version}.src.tar.xz
tarname_compilerrt=compiler-rt-${version}.src.tar.xz
tarname_cmake=cmake-${version}.src.tar.xz
tarname_libunwind=libunwind-${version}.src.tar.xz

# Remote source(s)
fetch="
 https://github.com/llvm/llvm-project/releases/download/llvmorg-${version}/$tarname
 https://github.com/llvm/llvm-project/releases/download/llvmorg-${version}/$tarname_clang
 https://github.com/llvm/llvm-project/releases/download/llvmorg-${version}/$tarname_compilerrt
 https://github.com/llvm/llvm-project/releases/download/llvmorg-${version}/$tarname_cmake
 https://github.com/llvm/llvm-project/releases/download/llvmorg-${version}/$tarname_libunwind
"

homepage=https://llvm.org
license="Apachev2 + LLVM exceptions"

description="
The LLVM compiler.

The LLVM Project is a collection of modular and reusable compiler and
toolchain technologies. Despite its name, LLVM has little to do with
traditional virtual machines.  The name \"LLVM\" itself is not an
acronym; it is the full name of the project.

For more information, visit: $homepage
"

# Source documentation
docs="CREDITS.TXT LICENSE.TXT README.txt"
docsdir="${docdir}/${program}-${version}"

# Source directory used by this software
srcdir=${program}-${version}.src

build()
{
    unpack "${tardir}/$tarname"

    unpack "${tardir}/$tarname_cmake"
    mv -f cmake-${version}.src cmake

    cd "$srcdir"

    cd tools

    unpack "${tardir}/$tarname_clang"
    mv clang-${version}.src clang

    cd ../projects

    unpack "${tardir}/$tarname_compilerrt"
    mv compiler-rt-${version}.src compiler-rt

    unpack "${tardir}/$tarname_libunwind"
    mv libunwind-${version}.src libunwind

    cd ..
    unset -v tarname_clang tarname_compilerrt tarname_cmake tarname_libunwind

    # Set sane permissions
    chmod -R u+w,go-w,a+rX-s .

    # Make sure to point to Python version 3 (Thanks to BLFS!)
    grep -rl '#!.*python' | xargs sed -i '1s/python$/python3/'

    # Apply a patch from "Alpine Linux"
    patch -p2 < "${worktree}/patches/llvm/0001-Disable-dynamic-lib-tests-for-musl-s-dlclose-is-noop.patch"

    # Another patch from "Alpine Linux" in order to set the installation prefix, *sigh*
    patch -p2 < "${worktree}/patches/llvm/install-prefix.patch"

    # Enable SSP in Clang by default (Thanks to BLFS!)
    (
        cd tools/clang && \
         patch -Np2 < "${worktree}/patches/llvm/clang-15.0.6-enable_default_ssp-1.patch"
    )

    # More patches taken from "Chimera Linux" in order to fix ("hell of") build issues
    (
        cd projects && \
         patch -p1 < "${worktree}/patches/llvm/chimera/0022-HACK-force-link-sanitizers-with-libexecinfo.patch"
    )

    rm -rf BUILD
    mkdir BUILD
    cd BUILD

    # Thanks to "Slackware Linux" for this instructions
    mkdir -p include
    # Copy this LLVM libunwind header or it won't be found:
    cp -Rpf ../projects/libunwind/include/mach-o include
    # Nuke LLVM libunwind as it conflicts with the one already on the system:
    rm -r ../projects/libunwind

    cmake \
     -DCMAKE_C_COMPILER="gcc"                                              \
     -DCMAKE_CXX_COMPILER="g++"                                            \
     -DCMAKE_C_FLAGS_RELEASE:STRING="$QICFLAGS"                            \
     -DCMAKE_CXX_FLAGS_RELEASE:STRING="$QICXXFLAGS"                        \
     -DCMAKE_EXE_LINKER_FLAGS:STRING="$QILDFLAGS"                          \
     -DCMAKE_SHARED_LINKER_FLAGS:STRING="$QILDFLAGS"                       \
     -DCMAKE_INSTALL_PREFIX=/usr                                           \
     -DLLVM_LIBDIR_SUFFIX=${libSuffix}                                     \
     -DLLVM_BINUTILS_INCDIR=/usr/include                                   \
     -DLLVM_TARGETS_TO_BUILD="host;AMDGPU;BPF"                             \
     -DLLVM_USE_LINKER=gold                                                \
     -DCMAKE_INSTALL_MANDIR=$mandir                                        \
     -DCMAKE_INSTALL_DOCDIR=$docsdir                                       \
     -DCMAKE_BUILD_TYPE=Release                                            \
     -DLLVM_BUILD_LLVM_DYLIB=ON                                            \
     -DLLVM_LINK_LLVM_DYLIB=ON                                             \
     -DCLANG_LINK_CLANG_DYLIB=ON                                           \
     -DLLVM_ENABLE_FFI=ON                                                  \
     -DLLVM_ENABLE_RTTI=ON                                                 \
     -DLLVM_ENABLE_ZLIB=ON                                                 \
     -DLLVM_INSTALL_UTILS=ON                                               \
     -DLLVM_ENABLE_ASSERTIONS=OFF                                          \
     -DLLVM_APPEND_VC_REV=OFF                                              \
     -DLLVM_PARALLEL_COMPILE_JOBS=$jobs                                    \
     -DLLVM_INCLUDE_BENCHMARKS=OFF                                         \
     -DCOMPILER_RT_BUILD_LIBFUZZER=OFF                                     \
     -G Ninja ..

    ninja -j${jobs}
    DESTDIR="$destdir" ninja -j${jobs} install

    # Strip ELF executables only excluding shared objects
    find "$destdir" -type f | xargs file | awk '/ELF/ && /executable/' | \
     cut -f 1 -d : | xargs strip --strip-unneeded 2> /dev/null || true

    # Strip static libraries in order to reduce the package size
    find "$destdir" -type f | xargs file | awk '/current ar archive/' | \
     cut -f 1 -d : | xargs strip --strip-debug 2> /dev/null || true

    # Compress and link man pages (if needed)
    if test -d "${destdir}/$mandir"
    then
        (
            cd "${destdir}/$mandir"
            find . -type f -exec lzip -9 {} +
            find . -type l | while read -r file
            do
                ln -sf "$(readlink -- "$file").lz" "${file}.lz"
                rm -- "$file"
            done
        )
    fi

    cd ../

    # Copy documentation
    mkdir -p "${destdir}/$docsdir"
    cp -p $docs "${destdir}/$docsdir"
}

