pkgname=compiler-rt
pkgver=20.1.8
pkgdesc="Compiler runtime libraries for clang"
arch=('x86_64')
homepage="https://compiler-rt.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=("compiler-rt-${pkgver}.src.tar.xz"
  "llvm-cmake-${pkgver}.src.tar.xz"
  "llvm-third-party-${pkgver}.src.tar.xz"
)
urls=("${_source_base}/${sources[0]}"
  "${_blfs_source_base}/${sources[1]}"
  "${_blfs_source_base}/${sources[2]}"
)
md5sums=("3869861662d173ca8303b9f1524d1e91"
  "5bfb8f4b4a2b3ccffca0d2406e4cdcc6"
  "2ffd8624b3cbddf55a4e74a7d8ea89fa"
)

src_prepare() {
  tar -xf ${distdir}/${sources[0]} &&
  tar -xf ${distdir}/${sources[1]} &&
  tar -xf ${distdir}/${sources[2]} &&
  mv cmake{-$pkgver.src,}
  mv third-party{-$pkgver.src,}
  cd compiler-rt-$pkgver.src
  mkdir build
  cd ..
}

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

  local cmake_args=(
    -G Ninja
    -DCMAKE_BUILD_TYPE=Release
    -DCMAKE_INSTALL_PREFIX=/usr
    -DCMAKE_SKIP_INSTALL_RPATH=ON
    -DCOMPILER_RT_INSTALL_PATH=/usr/lib/clang/${pkgver%%.*}
  )
  cmake .. "${cmake_args[@]}" &&
  ninja &&
  popd
}

src_install() {
  cd compiler-rt-$pkgver.src/build

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