# Maintainer: PktSurf <smlinux@pktsurf.in>
app=llvm
version=15.0.7
build=2sml
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
  
  (
    cd ..
    tar xf $srcdir/cmake-$version.src.tar.xz
    cp -r cmake-$version.src cmake
  )
  
  (
    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
  )
  
  applypatch $srcdir/lfs64.patch
}

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

  # Set build targets
  if [[ $arch = x86_64 ]] ; then
    buildtarget='X86;AMDGPU;BPF'
  elif [[ $arch = aarch64 ]] ; then
    buildtarget="AArch64"
  fi
  
  cd smbuild
  cmake .. -G Ninja \
    -DCMAKE_BUILD_TYPE=Release \
    -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_ENABLE_LIBEDIT=OFF \
    -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 $MAKEFLAGS
  DESTDIR="$pkg" ninja install

  cp ../LICENSE.TXT $pkgdocs/

  mkfinalpkg
}

sha512sums="
9fd2736a9f5993ddbb5b3c507fe497234a6def64f6f418f379d1ca56c9c361ad1ae9a5445ab938230fbc1671ec00b4f802a412b048569023863c20dc6bb46a1f  clang-15.0.7.src.tar.xz
85097a6eaee0df004567905f7e5ce8ca02e3b46c6fc66fa552fe99734642e6ee61685de4e5e6e8b505963334d7b91d98ecd9c5870e6bdd3f0018ee5b963ffa96  cmake-15.0.7.src.tar.xz
ebc73a5c68615a4462f4d05040a68a28e92b144d8c66d3fb2271cf6fff60a0adb1b913c8e520717ac2f6bd9e4b44c16d9461ef70502975975d1779e2b27a9392  libunwind-15.0.7.src.tar.xz
5dc0690c1eb3c37ee29c4add65a08f7b93ce1363cf9099243c9dc1124d1ca77fe19bbf24dad3984de4553eda716f706d49dff63461b709bec6645272e67f0178  lld-15.0.7.src.tar.xz
ed8d565515b1bc6164e4ff06d3388ba92e332850305496fd65db9adf1ec87bd9dd1bfde49dd41be5d5216289efc72bfd287cd7392f2bba80b740d4c314c749e5  llvm-15.0.7.src.tar.xz
e288e8d3389536f1f040c0ae8a8eae168de8eedba6040efd5f477701e18529711329a279c6b86d5214ab44efaa6e27818d109be78891ee93e241a0ab1b3a0c5a  lfs64.patch
"
