# POWER Maintainer: Alexander Baldeck <alex.bldck@gmail.com>
# Maintainer: Jürgen Hötzel <juergen@archlinux.org>

pkgbase='ocaml'
pkgname=('ocaml' 'ocaml-compiler-libs')
pkgver=4.14.2
pkgrel=1.1
pkgdesc="A functional language with OO extensions"
arch=(x86_64 powerpc64le powerpc64 powerpc riscv64)
license=('LGPL2.1' 'custom: QPL-1.0')
url="https://caml.inria.fr/"
makedepends=('ncurses>=5.6-7')
optdepends=('ncurses: advanced ncurses features' 'tk: advanced tk features')
source=(https://caml.inria.fr/distrib/ocaml-${pkgver%.*}/${pkgname}-${pkgver}.tar.xz
        ocaml-4.08.0-elfv2-ppc64.patch)
sha512sums=('acd0c0434d3e98fd97c22b77cabdb02f46e5b68504b751e0fce4886b35b751c1ac89030d380c2b94b096b73ea96b419168c4ecd1b4c8651535cae93de0e81e58'
            'a6dd3a2cfef21c1df7286ec2f4ad6ca48deac1f450232e7533a46e00867b0b1521d228345b5ee7212dc86c8e39295d152aed27c3e7f374b4c6a5595c425422a6')
options=('!makeflags' '!emptydirs' 'staticlibs')

prepare() {
  cd "${srcdir}/${pkgname}-${pkgver}"
  patch -Np1 -i ${srcdir}/ocaml-4.08.0-elfv2-ppc64.patch
}

build() {
  cd "${srcdir}/${pkgname}-${pkgver}"
  CFLAGS+=' -ffat-lto-objects'
  CXXFLAGS+=' -ffat-lto-objects'

  case "${CARCH}" in
    x86_64) _configure_flags=(--enable-frame-pointers)
  esac

  ./configure --prefix /usr --mandir /usr/share/man --disable-force-safe-string ${_configure_flags[@]}
  make --debug=v world.opt
}

package_ocaml() {
  cd "${srcdir}/${pkgbase}-${pkgver}"
  make DESTDIR="${pkgdir}" install

  # Save >10MB with this one, makepkg only strips debug symbols.
  #find "${pkgdir}/usr/lib" -type f -name '*.so.*' -exec strip --strip-unneeded {} \;

  # install license
  install -m755 -d "${pkgdir}/usr/share/licenses/${pkgname}"
  install -m644 LICENSE "${pkgdir}/usr/share/licenses/${pkgname}/"

  # remove compiler libs
  rm -rf "${pkgdir}/usr/lib/ocaml/compiler-libs"
}

package_ocaml-compiler-libs() {
pkgdesc="Several modules used internally by the OCaml compiler"
license=('custom: QPL-1.0')
depends=('ocaml')
optdepends=()

  cd "${srcdir}/${pkgbase}-${pkgver}"
  make DESTDIR="${pkgdir}" install
  # Remove non-compiler-libs
  rm -rf   "${pkgdir}/usr/bin"  "${pkgdir}/usr/lib/ocaml/caml" \
     "${pkgdir}/usr/lib/ocaml/ocamldoc" "${pkgdir}/usr/lib/ocaml/stublibs" \
     "${pkgdir}/usr/lib/ocaml/threads" "${pkgdir}/usr/share"
  find "${pkgdir}/usr/lib/ocaml/" -maxdepth 1 -type f -delete

  install -m755 -d "${pkgdir}/usr/share/licenses/${pkgname}"
  install -m644 LICENSE "${pkgdir}/usr/share/licenses/${pkgname}/"
}
