%_ghclibdir	%_libdir/ghc-%ghc_version

# Emulating the symlinks for some of the common (unversioned) names
# -- for those ones that are used here internally in the macros.
#
# This will allow to build an SRPM even if it doesn't build-require
# ghc or ghc%%{ghc_version}-common. It's the case in some legacy SRPMs.
#
# Although a cleaner way would be for the SRPM to simply build-require
# either ghc for building with most recent GHC or ghc%{ghc_version}-common
# for building with a fixed legacy version of GHC.
# And to invoke the unversioned commands if it needs to.
# (That's why these macros are "internal", with %%__.)
#
# That's not very dirty, because we need to know %%ghc_version anyway
# in order to put the build Haskell modules into the correct place
# (i.e., %%_ghclibdir, which is used below).
%__runghc		runghc-%ghc_version
%__ghc			ghc-%ghc_version

%__hs_build_setup [ -x "Setup" ] || %__ghc Setup

# In november 2018 for ghc7.6.1 building for these arches is available
%ghc_arches   %ix86 x86_64 mipsel aarch64 ppc64le

%hs_libdir %_ghclibdir/lib
%hs_pkgconfdir %_ghclibdir/package.conf.d
%hs_setup %__hs_build_setup; \
./Setup %nil

%hs_configure  configure

# internal macro, cloned from rpm-build-python's py_package_deps_internal
%__hs_package_deps_internal() \
args="%{*}"; \
set $args ''; \
while test $# -gt 1; do \
  mod=${1%%,}; shift; \
  if test $# -gt 1; then \
    if test "x$1" = "x<" -o "x$1" = "x<=" -o "x$1" = "x=" -o "x$1" = "x>=" -o "x$1" = "x>"; then \
      verstr="$1 $2"; shift; shift; \
    fi; \
  fi; \
  echo -n "ghc(${mod}) $verstr "; \
  unset mod; unset verstr; \
done; \
%nil


# hs_package_dependencies <hspackage1> [[>= <hspackage1_version>] [<hspackage2> ...]]

%hs_package_dependencies() %(%{expand: %%__hs_package_deps_internal %{*}})

%hs_gen_filelist \
%_libexecdir/rpm-build-haskell/hs_gen_filelist.sh  %buildroot %_libdir %pkg_libdir  %_builddir/%{?buildsubdir}  %name  %version %hs_pkgconfdir/%f_pkg_name-%version.conf; \
%nil

%hs_build \
%__hs_build_setup \
if ./Setup makefile -f cabal-rpm.mk; \
then \
    %make_build -f cabal-rpm.mk || : ; \
fi; \
./Setup build; \
./Setup haddock || : ;\
./Setup register --gen-pkg-config=%f_pkg_name.pkg; \
%nil

%hs_configure2 \
if [ -f configure.ac -a ! -f configure ]; then autoreconf; fi; \
%__hs_build_setup \
./Setup configure \\\
    --prefix=%_prefix \\\
    --libdir=%_libdir \\\
    --docdir=%_docdir/%hsc_namever-%f_pkg_name-%version \\\
    %if "%(rpmvercmp '%ghc_version' '8.0.0')" >= "0" \
    --libsubdir='$compiler/$pkgid' \\\
    --dynlibdir='%_libdir/$compiler/lib' \\\
    %else \
    --libsubdir='$compiler/lib/$pkgid' \\\
    %endif \
    --docdir=%_docdir/%name-%version \\\
    --datadir=%_datadir \\\
    --datasubdir=%name-%version \\\
    --enable-library-profiling \\\
    --enable-library-for-ghci \\\
    --disable-executable-profiling \\\
    --enable-split-objs \\\
    --enable-shared \\\
     \\\
    -O2 \\\
    --ghc %nil

%hs_install \
%__hs_build_setup \
./Setup copy --destdir=%buildroot; \
if [ -f %f_pkg_name.pkg ]; then \
    install -m 755 %f_pkg_name.pkg %buildroot%pkg_libdir; \
    mkdir -p %buildroot%hs_pkgconfdir; \
    install -D -m 644 %f_pkg_name.pkg %buildroot%hs_pkgconfdir/%f_pkg_name-%version.conf; \
fi \
%nil

%haskell_ABI_dep haskell(abi) = %ghc_version
%_haskell_compile_include_restricted %hs_libdir

%__haskell_runtime %_ghclibdir/rts/libHSrts-ghc%ghc_version.so
%_files_to_ld_preload_haskell_runtime %files_requiring_haskell_ABI%(for d in %_haskell_compile_include_restricted; do echo -n " $d/*"; done)
%files_requiring_haskell_ABI %nil

# libHSrts*.so (which is to be ld_preloaded) resides in ghc%ghc_version
%requires_hs_ABI_for_files() \
BuildPreReq: ghc%ghc_version >= 8.6.3-alt3 \
Requires: %haskell_ABI_dep \
%global files_requiring_haskell_ABI %files_requiring_haskell_ABI %* \
%nil
