%_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 \\\
    %if "%(rpmvercmp '%ghc_version' '9.0.0')" < "0" \
    %{native_code_gen_split_objs} \\\
    %else \
    --enable-split-sections \\\
    %endif \
     \\\
    --ghc-option=-DALT_NO_GHCI --flags=-templateHaskell \\\
    -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

# NEW MACROSES for ghc >= 9.0
# hs_* gonna be absoleted in next versions
%ghclibdir %_ghclibdir/lib
%ghc_pkgconfdir %ghclibdir/package.conf.d
%__ghc_gen_filelist %_libexecdir/rpm-build-haskell/ghc_gen_filelist.sh

%ghc_smp_mflags %{?_smp_mflags}

# If there is no Setup.hs file, we create it manually
%__ghc_init_setup %{expand:
    [ -f "Setup.hs" ] || printf "import Distribution.Simple\\\nmain = defaultMain\\\n" > Setup.hs

    %__ghc --make -package Cabal -no-user-package-db Setup
}

%cabal %{expand:
    if [ ! -x Setup ]; then
        %__ghc_init_setup
    fi
    ./Setup \\\
}

%cabal_configure %{expand:
%cabal  %{shrink: configure
        --prefix=%_prefix
        --libdir=%_libdir
        --libsubdir='$compiler/lib/$arch-$os-$compiler/$pkgid'
        --dynlibdir='%_libdir/$compiler/lib/$arch-$os-$compiler'
        --docdir='%_docdir/$compiler/libraries/$pkgid'
        --datadir=%_datadir
        --datasubdir='$compiler/$pkgid'
        %{?_with_tests: --enable-tests}
        --enable-split-sections
        --enable-shared
        -O2
        --ghc
        --with-ghc=%__ghc
        --disable-executable-stripping
        --disable-library-stripping
        --enable-debug-info=3
        %{?cabal_configure_options}
        }
}

%cabal_install %{expand:
    %cabal copy --destdir=%buildroot
}

%ghc_install_pkgconfig %{expand:
    PKGFILE=$(find %_builddir -name "*.conf" | head -n 1);
    if [ -e "$PKGFILE" ]; then
        PKGID="$(basename $PKGFILE | sed 's|.conf||')";
        printf "$PKGID" > %name.pkgid
        PKG_LIBDIR=$(find "%buildroot%ghclibdir" -name "$PKGID" -type d | sed "s|%buildroot||");

        mkdir -p "%buildroot$PKG_LIBDIR";
        mkdir -p "%buildroot%ghc_pkgconfdir";

        if [ -f "$PKGFILE" ]; then
            install -m 644 -t "%buildroot$PKG_LIBDIR" "$PKGFILE";
            install -m 644 -t "%buildroot%ghc_pkgconfdir" "$PKGFILE";
        elif [ -d "$PKGFILE" ]; then
            for i in $(ls $PKGFILE); do
                sub=$(echo $i | sed -e "s/^[0-9]\\+-//");
                install -m 644 "$PKGFILE/$i" "%buildroot$PKG_LIBDIR/$sub.conf";
                install -m 644 "$PKGFILE/$i" "%buildroot%ghc_pkgconfdir/$sub.conf";
            done;
        else
            echo "Could not find $PKGFILE, aborting install";
            exit 1;
        fi;
        rename ".conf" ".pkg" "%buildroot$PKG_LIBDIR"/*;
    fi;
}

%ghc_delete_rpaths %{expand:
    find %buildroot -type f | xargs -n 1 file | grep 'dynamically linked' | cut -f1 -d: \\\
                            | xargs -n 1 chrpath -d
}

%ghc_lib_build %{expand:
    %cabal_configure
    %cabal build %{?ghc_smp_mflags}
    %if_with docs
        %cabal haddock --html --hyperlink-source --hoogle %{?cabal_haddock_options}
    %endif
    %cabal register --gen-pkg-config
}

%ghc_lib_install %{expand:
    %cabal_install
    %ghc_install_pkgconfig
    %ghc_delete_rpaths
}

%ghc_bin_build %{expand:
    %cabal_configure
    %cabal build %{?ghc_smp_mflags}
}

%ghc_bin_install %{expand:
    %cabal_install
    %ghc_delete_rpaths
}

%cabal_test %cabal test %{?cabal_test_options}

%ghc_gen_filelist %{shrink:
    %__ghc_gen_filelist %buildroot %_libdir %_ghclibdir
                        %_builddir/%{?buildsubdir}
                        %_docdir %_datadir
}
