pkgname=llvm
pkgver=20.1.8
arch=('x86_64')
url="https://llvm.org/"
license=('Apache-2.0 WITH LLVM-exception')
_source_base=https://github.com/llvm/llvm-project/releases/download/llvmorg-$pkgver
sources=("${pkgname}-${pkgver}.src.tar.xz"
  "llvm-cmake-${pkgver}.src.tar.xz"
  "llvm-third-party-${pkgver}.src.tar.xz"
)
urls=("${_source_base}/llvm-${pkgver}.src.tar.xz"
  "https://anduin.linuxfromscratch.org/BLFS/llvm/llvm-cmake-${pkgver}.src.tar.xz"
  "https://anduin.linuxfromscratch.org/BLFS/llvm/llvm-third-party-${pkgver}.src.tar.xz"
)
md5sums=("78040509eb91309b4ec2edfe12cd20d8"
  "5bfb8f4b4a2b3ccffca0d2406e4cdcc6"
  "2ffd8624b3cbddf55a4e74a7d8ea89fa"
)

src_prepare() {
  tar -xf ${distdir}/${sources[0]} --strip-components=1 &&
  tar -xf ${distdir}/${sources[1]} &&
  tar -xf ${distdir}/${sources[2]} &&
  sed "/LLVM_COMMON_CMAKE_UTILS/s@../cmake@cmake-${pkgver}.src@"          \
      -i CMakeLists.txt                                                  &&
  sed "/LLVM_THIRD_PARTY_DIR/s@../third-party@third-party-${pkgver}.src@" \
      -i cmake/modules/HandleLLVMOptions.cmake                           &&
  sed 's/utility/tool/' -i utils/FileCheck/CMakeLists.txt
}

# Utilizing LLVM_DISTRIBUTION_COMPONENTS to avoid
# installing static libraries; inspired by Gentoo
_get_distribution_components() {
  local target
  ninja -t targets | grep -Po 'install-\K.*(?=-stripped:)' | while read -r target; do
    case $target in
      llvm-libraries|distribution)
        continue
        ;;
      # shared libraries
      LLVM|LLVMgold)
        ;;
      # libraries needed for clang-tblgen
      LLVMDemangle|LLVMSupport|LLVMTableGen)
        ;;
      # used by lldb
      LLVMDebuginfod)
        ;;
      # testing libraries
      LLVMTestingAnnotations|LLVMTestingSupport)
        ;;
      # exclude static libraries
      LLVM*)
        continue
        ;;
      # exclude llvm-exegesis (doesn't seem useful without libpfm)
      llvm-exegesis)
        continue
        ;;
    esac
    echo $target
  done
}

src_build() {
  mkdir -v build &&
  cd       build &&

  # Build only minimal debug info to reduce size
  CFLAGS=${CFLAGS/-g /-g1 }
  CXXFLAGS=${CXXFLAGS/-g /-g1 }

  local cmake_args=(
    -D CMAKE_INSTALL_PREFIX=/usr
    -D CMAKE_SKIP_INSTALL_RPATH=ON
    -D LLVM_ENABLE_FFI=ON
    -D LLVM_USE_PERF=ON
    -D CMAKE_BUILD_TYPE=Release
    -D LLVM_BUILD_LLVM_DYLIB=ON
    -D LLVM_LINK_LLVM_DYLIB=ON
    -D LLVM_ENABLE_RTTI=ON
    -D LLVM_ENABLE_SPHINX=OFF
    -D LLVM_TARGETS_TO_BUILD="host;AMDGPU"
    -D LLVM_BINUTILS_INCDIR=/usr/include
    -D LLVM_INCLUDE_BENCHMARKS=OFF
    -D LLVM_BUILD_TESTS=ON
    -D LLVM_INSTALL_GTEST=ON
    -W no-dev
    -G Ninja
  )

  CC=gcc CXX=g++ cmake .. "${cmake_args[@]}"    &&
  local distribution_components=$(_get_distribution_components | paste -sd\;) &&
  test -n "$distribution_components" &&
  cmake_args+=(-DLLVM_DISTRIBUTION_COMPONENTS="$distribution_components") &&

  CC=gcc CXX=g++ cmake .. "${cmake_args[@]}" &&

  ninja &&
  cd .. &&

  pushd utils/lit &&
  pip3 wheel -w dist --no-build-isolation --no-deps $PWD &&
  popd
}

src_check() {
  pushd build &&
  ninja check-llvm &&
  popd
}

src_install() {
  pushd build &&
  DESTDIR="$pkgdir" ninja install-distribution &&
  pushd ../utils/lit &&
  pip3 install --no-deps --no-warn-script-location --no-index --find-links dist --no-cache-dir --no-user --root $pkgdir lit &&
  popd &&
  # Remove documentation sources
  rm -r "$pkgdir"/usr/share/doc/llvm/html/{_sources,.buildinfo} || true &&
  install -Dm644 ../LICENSE.TXT "$pkgdir/usr/share/licenses/$pkgname/LICENSE" &&
  popd
}
