pkgname=clang
pkgver=20.1.8
pkgdesc="C language family frontend for LLVM"
arch=('x86_64')
homepage="https://clang.llvm.org/"
license=('Apache-2.0 WITH LLVM-exception')
_source_base=https://github.com/llvm/llvm-project/releases/download/llvmorg-$pkgver
_blfs_source_base="https://anduin.linuxfromscratch.org/BLFS/llvm/"
sources=("clang-$pkgver.src.tar.xz"
  "clang-tools-extra-$pkgver.src.tar.xz"
  "llvm-${pkgver}.src.tar.xz"
  "llvm-cmake-${pkgver}.src.tar.xz"
  "llvm-third-party-${pkgver}.src.tar.xz"
)
urls=("${_source_base}/${sources[0]}"
  "${_source_base}/${sources[1]}"
  "${_blfs_source_base}/${sources[2]}"
  "${_blfs_source_base}/${sources[3]}"
  "${_blfs_source_base}/${sources[4]}"
)
md5sums=("62a0500bb932868061607cde0c01f584"
  "254c99f104cd17b32ef721acaeb58a7c"
  "78040509eb91309b4ec2edfe12cd20d8"
  "5bfb8f4b4a2b3ccffca0d2406e4cdcc6"
  "2ffd8624b3cbddf55a4e74a7d8ea89fa"
)

src_prepare() {
  tar -xf ${distdir}/${sources[0]} &&
  tar -xf ${distdir}/${sources[1]} &&
  tar -xf ${distdir}/${sources[2]} &&
  tar -xf ${distdir}/${sources[3]} &&
  tar -xf ${distdir}/${sources[4]} &&
  rename -v -- "-$pkgver.src" '' {cmake,clang-tools-extra}-$pkgver.src
  cd clang-$pkgver.src
  mkdir build
  cd ..
}

# 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
      clang-libraries|distribution)
        continue
        ;;
      clang|clangd|clang-*)
        ;;
      clang*|findAllSymbols)
        continue
        ;;
    esac
    echo $target
  done
}

src_build() {
  pushd clang-$pkgver.src/build

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

  local cmake_args=(
    -G Ninja
    -DCMAKE_BUILD_TYPE=Release
    -DCMAKE_INSTALL_PREFIX=/usr
    -DCMAKE_INSTALL_DOCDIR=share/doc
    -DCMAKE_SKIP_RPATH=ON
    -DCLANG_DEFAULT_PIE_ON_LINUX=ON
    -DCLANG_LINK_CLANG_DYLIB=ON
    -DENABLE_LINKER_BUILD_ID=ON
    -DLLVM_BUILD_TESTS=ON
    -DLLVM_ENABLE_RTTI=ON
    -DLLVM_ENABLE_SPHINX=OFF
    -DLLVM_EXTERNAL_CLANG_TOOLS_EXTRA_SOURCE_DIR="$srcdir/clang-tools-extra"
    -DLLVM_EXTERNAL_LIT=/usr/bin/lit
    -DLLVM_LINK_LLVM_DYLIB=ON
    -DLLVM_THIRD_PARTY_DIR="$srcdir/third-party-$pkgver.src"
  )

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

  cmake .. "${cmake_args[@]}" &&
  ninja &&
  popd
}

src_check() {
  pushd clang-$pkgver.src/build
  LD_LIBRARY_PATH=$PWD/lib ninja check-clang{,-tools}
  popd
}

_python_optimize() {
  python -m compileall "$@"
  python -O -m compileall "$@"
  python -OO -m compileall "$@"
}

src_install() {
  pushd clang-$pkgver.src/build

  DESTDIR="$pkgdir" ninja install-distribution
  install -Dm644 ../LICENSE.TXT "$pkgdir/usr/share/licenses/$pkgname/LICENSE"

  mkdir -pv ${pkgdir}/etc/clang &&
  for i in clang clang++; do
    echo -fstack-protector-strong > ${pkgdir}/etc/clang/$i.cfg
  done

  # Remove documentation sources
  rm -r "$pkgdir"/usr/share/doc/clang{,-tools}/html/{_sources,.buildinfo} || true

  # Move scanbuild-py into site-packages and install Python bindings
  local site_packages=$(python -c "import site; print(site.getsitepackages()[0])")
  install -d "$pkgdir/$site_packages"
  mv "$pkgdir"/usr/lib/{libear,libscanbuild} "$pkgdir/$site_packages/"
  cp -a ../bindings/python/clang "$pkgdir/$site_packages/"

  # Move analyzer scripts out of /usr/libexec
  mv "$pkgdir"/usr/libexec/* "$pkgdir/usr/lib/clang/"
  rmdir "$pkgdir/usr/libexec"
  sed -i 's|libexec|lib/clang|' \
    "$pkgdir/usr/bin/scan-build" \
    "$pkgdir/$site_packages/libscanbuild/analyze.py"

  # Compile Python scripts
  _python_optimize "$pkgdir/usr/share" "$pkgdir/$site_packages"

  # Move bash completion
  local bash_completion_destdir="$pkgdir/usr/share/bash-completion/completions"
  install -d $bash_completion_destdir
  mv "$pkgdir/usr/share/clang/bash-autocomplete.sh" "$bash_completion_destdir/clang"

  popd
}
