# Maintainer: PktSurf <smlinux@pktsurf.in>
app=llvm
version=14.0.6
build=1sml
homepage="http://llvm.org/"
download="https://github.com/llvm/llvm-project/releases/download/llvmorg-$version/llvm-$version.src.tar.xz"
desc="LLVM compiler toolkit"
requires="libffi libxml2 perl"

prepbuilddir() {
  mkandenterbuilddir
  rm -rf $app-$version.src

  tar xf $srcdir/$app-$version.src.tar.?z*
  cd $app-$version.src 
  fixbuilddirpermissions
  
  # Set build targets
  if [ "$arch" = "x86_64" ] ; then
    buildtarget='X86;AMDGPU;BPF'
  elif [ "$arch" = "aarch64" ] ; then
    buildtarget="ARM;AArch64"
  fi

  (
    cd tools
    tar xf $srcdir/clang-$version.src.tar.?z 
    mv clang-$version.src clang
    tar xf $srcdir/lld-$version.src.tar.?z
    mv lld-$version.src lld
  )

  (
    cd projects
    tar xf $srcdir/libunwind-$version.src.tar.?z
    mv libunwind-$version.src libunwind
  )

  (
    mkdir -p smbuild && cd smbuild
    mkdir -p include

    cp -r ../projects/libunwind/include/mach-o include/
    rm -r ../projects/libunwind
  )
}

build() {
  # We don't need our optimisations for small builds
  unset CFLAGS CXXFLAGS

  cd smbuild
  cmake .. -G Ninja -Wno-dev \
    -DCMAKE_BUILD_TYPE=MinSizeRel \
    -DCMAKE_INSTALL_PREFIX="" \
    -DLLVM_BUILD_DOCS=OFF \
    -DLLVM_BUILD_EXAMPLES=OFF \
    -DBUILD_SHARED_LIBS=ON \
    -DLLVM_ENABLE_ASSERTIONS=OFF \
    -DLLVM_BUILD_TESTS=OFF \
    -DLLVM_ENABLE_FFI=ON \
    -DLLVM_ENABLE_RTTI=ON \
    -DLLVM_INSTALL_UTILS=ON \
    -DLLVM_TARGETS_TO_BUILD="$buildtarget" \
    -DLLVM_BINUTILS_INCDIR="/include" \
    -DLLVM_INCLUDE_BENCHMARKS=OFF \
    -DLLVM_INCLUDE_EXAMPLES=OFF \
    -DLLVM_INSTALL_PREFIX="/bin" \
    -DCLANG_RESOURCE_DIR="../lib/clang/$version"

  ninja
  DESTDIR="$pkg" ninja install

  cp ../LICENSE.TXT $pkgdocs/

  mkfinalpkg
}

sha512sums="
5f5497b57dd116225e90f321902f1015beb50c0b1bb90d0fc6c026f13aa748feabe44f98eb75e86f98f3b1785ad5e850210732f3eae75b942ecd520527dcd30b  clang-14.0.6.src.tar.xz
c8f3804c47ac33273238899e5682f9cb52465dcceff0e0ecf9925469620c6c9a62cc2c708a35a0e156b666e1198df52c5fff1da9d5ee3194605dfd62c296b058  libunwind-14.0.6.src.tar.xz
fad97b441f9642b73edd240af2c026259de0951d5ace42779e9e0fcf5e417252a1d744e2fc51e754a45016621ba0c70088177f88695af1c6ce290dd26873b094  lld-14.0.6.src.tar.xz
6461bdde27aac17fa44c3e99a85ec47ffb181d0d4e5c3ef1c4286a59583e3b0c51af3c8081a300f45b99524340773a3011380059e3b3a571c3b0a8733e96fc1d  llvm-14.0.6.src.tar.xz
"
