# Maintainer: Joshua Warren <816217+joshuaswarren@users.noreply.github.com>

pkgname=mil-hwx-compiler
pkgver=0.1.0
pkgrel=1
pkgdesc='MIL-to-HWX compiler for the Apple Neural Engine (mil-hwxc)'
arch=('aarch64')
groups=('omarchy-platform-apple-silicon')
url='https://github.com/joshuaswarren/mil-hwx-compiler'
license=('MIT' 'LGPL-2.1-or-later')
depends=('gcc-libs' 'glibc' 'icu' 'libffi' 'libxml2' 'zlib')
makedepends=('clang' 'cmake' 'git' 'lld' 'ninja' 'robin-map')
checkdepends=('python')
# The compiler is Objective-C++ with ARC, so it needs the libobjc2 runtime and a
# gnustep-base built for it. Arch's gnustep-base uses gcc's libobjc. This
# package builds its own copy for $_prefix, from the commits that the
# release's scripts/bootstrap-linux-toolchain.sh pins.
_prefix=/usr/lib/mil-hwx-compiler
_objc_commit=a1faad66cee79f29c4ca268b004111edc33a2ae2
_make_commit=d0349cc83a23367acdc53df364f73504c281b26c
_base_commit=3d7013e144b153abcd6c088dc8d6c64ebd43bc4b
source=("$pkgname-$pkgver.tar.gz::$url/archive/refs/tags/v$pkgver.tar.gz"
        "libobjc2::git+https://github.com/gnustep/libobjc2.git#commit=$_objc_commit"
        "tools-make::git+https://github.com/gnustep/tools-make.git#commit=$_make_commit"
        "libs-base::git+https://github.com/gnustep/libs-base.git#commit=$_base_commit")
sha256sums=('9aa579212b5fddfc43c3438619ba7a41663459d26f1442a6105de6c7f1ba1772'
            '8b791f7e39275d74e98caa4436e859fbc89cbbaa5818fa2f63d301c71234d6e7'
            'dcc01f3da5b4c63bb841bae49d420c82342f667a88ec6e460206c1c991678277'
            '1b4a16e2148d4ca0e3985cbde0f898529bba1c7c85862eeffc8e122ab6e6adf4')

prepare() {
  # A release that moves the toolchain pins must move them here too.
  local bootstrap="$pkgname-$pkgver/scripts/bootstrap-linux-toolchain.sh"
  grep -qx "objc_commit=$_objc_commit" "$bootstrap"
  grep -qx "make_commit=$_make_commit" "$bootstrap"
  grep -qx "base_commit=$_base_commit" "$bootstrap"
}

# The toolchain builds into $srcdir/gnustep, the way the bootstrap script does.
# Only the runtime libraries ship, in $_prefix/lib.
_toolchain_env() {
  _gnustep="$srcdir/gnustep"
  export CC=clang CXX=clang++ OBJC=clang OBJCXX=clang++
  export CPPFLAGS="$CPPFLAGS -I$_gnustep/include"
  export LDFLAGS="$LDFLAGS -L$_gnustep/lib -Wl,-rpath,$_prefix/lib -fuse-ld=lld"
  export LD_LIBRARY_PATH="$_gnustep/lib" PATH="$_gnustep/bin:$PATH"
}

build() {
  _toolchain_env

  cmake -S libobjc2 -B build-libobjc2 -G Ninja \
    -DCMAKE_BUILD_TYPE=Release \
    -DCMAKE_INSTALL_PREFIX="$_gnustep" \
    -DTESTS=OFF
  cmake --build build-libobjc2
  cmake --install build-libobjc2

  (
    cd tools-make
    ./configure --prefix="$_gnustep" --with-layout=fhs \
      --with-library-combo=ng-gnu-gnu --with-runtime-abi=gnustep-2.0 \
      --enable-objc-arc --enable-native-objc-exceptions
    make
    make install
  )

  # gnustep-base compiles in the paths of a GNUstep.conf for when it finds no
  # config file at run time. Give it the installed paths. The package ships no
  # GNUstep.conf.
  sed "s|$_gnustep|$_prefix|g" "$_gnustep/etc/GNUstep/GNUstep.conf" >GNUstep-installed.conf
  (
    export GNUSTEP_CONFIG_FILE="$_gnustep/etc/GNUstep/GNUstep.conf"
    . "$_gnustep/share/GNUstep/Makefiles/GNUstep.sh"
    cd libs-base
    ./configure --prefix="$_gnustep" \
      --with-config-file="$_prefix/etc/GNUstep/GNUstep.conf" \
      --with-default-config="$srcdir/GNUstep-installed.conf" \
      --disable-tls --disable-xslt --disable-zeroconf --disable-libdispatch \
      --disable-nsurlsession
    make
    make install
  )

  make -C "$pkgname-$pkgver" build/mil-hwxc \
    CXX="clang++ -Wl,-rpath,$_prefix/lib" \
    GNUSTEP_PREFIX="$_gnustep" GNUSTEP_CONFIG="$_gnustep/bin/gnustep-config"
}

check() {
  _toolchain_env
  cd "$pkgname-$pkgver"
  local mode
  for mode in software-tests emit hygiene; do
    GNUSTEP_PREFIX="$_gnustep" scripts/verify-linux-compiler.sh "$mode"
  done
}

package() {
  local lib="$srcdir/gnustep/lib"
  install -d "$pkgdir$_prefix/lib"
  cp -P "$lib"/libobjc.so* "$lib"/libgnustep-base.so* "$pkgdir$_prefix/lib/"
  # gnustep-base's resources: languages, time zones, DTDs.
  cp -r --no-preserve=ownership "$lib/GNUstep" "$pkgdir$_prefix/lib/"

  install -Dm755 "$pkgname-$pkgver/build/mil-hwxc" "$pkgdir/usr/bin/mil-hwxc"
  install -Dm644 "$pkgname-$pkgver/LICENSE" "$pkgdir/usr/share/licenses/$pkgname/LICENSE"
  install -Dm644 libobjc2/COPYING "$pkgdir/usr/share/licenses/$pkgname/libobjc2-COPYING"
  install -Dm644 libs-base/COPYING.LIB "$pkgdir/usr/share/licenses/$pkgname/libs-base-COPYING.LIB"
}
