##
##  ghc.spec -- OpenPKG RPM Package Specification
##  Copyright (c) 2000-2025 OpenPKG Project <http://openpkg.org/>
##
##  Permission to use, copy, modify, and distribute this software for
##  any purpose with or without fee is hereby granted, provided that
##  the above copyright notice and this permission notice appear in all
##  copies.
##
##  THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
##  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
##  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
##  IN NO EVENT SHALL THE AUTHORS AND COPYRIGHT HOLDERS AND THEIR
##  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
##  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
##  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
##  USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
##  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
##  OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
##  OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
##  SUCH DAMAGE.
##

#   package version
%define       V_source   6.10.2
%define       V_boot_bsd 6.8.3
%define       V_boot_lnx 6.10.2
%define       V_boot_sol 6.10.2

#   package information
Name:         ghc
Summary:      Glasgow Haskell Compiler
URL:          http://haskell.org/ghc/
Vendor:       University of Glasgow
Packager:     OpenPKG Project
Distribution: OpenPKG Community
Class:        EVAL
Group:        Language
License:      X11-style
Version:      %{V_source}
Release:      20090503

#   list of sources
Source0:      http://haskell.org/ghc/dist/%{V_source}/ghc-%{V_source}-src.tar.bz2
Source1:      http://haskell.org/ghc/dist/%{V_source}/ghc-%{V_source}-src-extralibs.tar.bz2
Source2:      http://haskell.org/ghc/dist/%{V_boot_bsd}/FreeBSD/ghc-%{V_boot_bsd}-i386-unknown-freebsd6-boot.tar.bz2
Source3:      http://haskell.org/ghc/dist/%{V_boot_lnx}/ghc-%{V_boot_lnx}-i386-unknown-linux.tar.bz2
Source4:      http://haskell.org/ghc/dist/%{V_boot_sol}/maeder/ghc-%{V_boot_sol}-sparc-sun-solaris2.tar.bz2
Patch0:       ghc.patch

#   build information
BuildPreReq:  OpenPKG, openpkg >= 20160101, gcc, perl, make
PreReq:       OpenPKG, openpkg >= 20160101, gcc
BuildPreReq:  gmp
PreReq:       gmp

%description
    GHC is a state-of-the-art, open source, compiler and interactive
    environment for the functional language Haskell. GHC supports the
    entire Haskell 98 language plus a wide variety of extensions.
    GHC compiles Haskell code either by using an intermediate C
    compiler (GCC), or by generating native code on some platforms.
    The interactive environment compiles Haskell to bytecode, and
    supports execution of mixed bytecode/compiled programs. Profiling
    is supported, both by time/allocation and various kinds of heap
    profiling. GHC comes with a wide range of libraries.

%track
    prog ghc = {
        version   = %{V_source}
        url       = http://haskell.org/ghc/dist/
        regex     = (\d+\.\d+(\.\d+)?)
        url       = http://haskell.org/ghc/dist/__NEWVER__/
        regex     = ghc-(__VER__)-src\.tar\.bz2
    }

%prep
    %setup -q
    %setup -q -D -T -b 1
    %patch -p0

    #   remove libraries which potentially can make trouble
    for name in ALUT GLUT HGL OpenAL OpenGL X11; do
        rm -rf libraries/$name
    done

    #   optionally unpack bootstrapping distribution
    if [ ! -f %{l_prefix}/bin/ghc ]; then
        case "%{l_platform -t}" in
            i?86-freebsd*  ) boot="%{SOURCE2}" ;;
            amd64-freebsd* ) boot="%{SOURCE2}" ;;
            i?86-linux*    ) boot="%{SOURCE3}" ;;
            amd64-linux*   ) boot="%{SOURCE3}" ;;
            sun*-sunos*    ) boot="%{SOURCE4}" ;;
            * ) echo "platform \"%{l_platform -t}\" not supported"; exit 1 ;;
        esac
        %{l_bzip2} -d -c $boot | %{l_tar} xf -
        ( cd ghc-*-boot
          %{l_prefix}/bin/config install
          case "%{l_platform -t}" in
              amd64-freebsd* )
                  mv bin/i386-unknown-freebsd bin/x86_64-unknown-freebsd
                  mv lib/i386-unknown-freebsd lib/x86_64-unknown-freebsd
                  ;;
              amd64-linux* )
                  mv bin/i386-unknown-linux bin/x86_64-unknown-linux
                  mv lib/i386-unknown-linux lib/x86_64-unknown-linux
                  ;;
          esac
        ) || exit $?
    fi

%build
    #   determine bootstrapping installation
    if [ -f %{l_prefix}/bin/ghc ]; then
        #   use version from previous package release
        #   and perform a stage 1 build only
        l_ghc=%{l_prefix}/bin/ghc
        m_target="stage1 stage=1"
    else
        #   use the pre-built bootstrap distribution in-place
        #   and perform a full-featured stage 1 and stage 2 build
        l_boot="`pwd`/ghc-boot"
        ( cd ghc-*-boot
          ./configure --prefix="$l_boot"
          %{l_make} %{l_mflags}
          %{l_make} %{l_mflags} install
        ) || exit $?
        l_ghc="$l_boot/bin/ghc"
        m_target="bootstrap"
        PATH="$l_boot/bin:$PATH"
    fi

    #   workaround build-time ~/.ghc/ storage
    export HOME=`pwd`

    #   configure program
    ( echo "GhcLibWays="
      echo "SplitObjs=NO"
    ) >>mk/build.mk
    CC="%{l_cc}" \
    CFLAGS="%{l_cflags -O}" \
    CPPFLAGS="%{l_cppflags}" \
    LDFLAGS="%{l_ldflags}" \
    PerlCmd=%{l_prefix}/bin/perl \
    ./configure \
        --prefix=%{l_prefix} \
        --with-ghc="${l_ghc}" \
        --with-gcc="%{l_cc}" \
        --with-gmp-includes=%{l_prefix}/include \
        --with-gmp-libraries=%{l_prefix}/lib

    #   build program
    %{l_make} %{l_mflags} $m_target

%install
    #   install program
    if [ -f %{l_prefix}/bin/ghc ]; then
        #   install the stage 1
        m_target="install stage=1"
    else
        #   install the stage 2
        m_target="install"
        l_boot="`pwd`/ghc-boot"
        PATH="$l_boot/bin:$PATH"
    fi
    %{l_make} %{l_mflags} $m_target DESTDIR=$RPM_BUILD_ROOT

    #   adjust and strip down installation
    for prog in ghc ghc-pkg ghci; do
        rm -f $RPM_BUILD_ROOT%{l_prefix}/bin/$prog
        mv  $RPM_BUILD_ROOT%{l_prefix}/bin/$prog-%{version} \
            $RPM_BUILD_ROOT%{l_prefix}/bin/$prog
    done
    %{l_shtool} subst \
        -e 's;bin/ghc-[0-9.]*;bin/ghc;' \
        $RPM_BUILD_ROOT%{l_prefix}/bin/ghci
    strip $RPM_BUILD_ROOT%{l_prefix}/bin/* >/dev/null 2>&1 || true
    rm -rf $RPM_BUILD_ROOT%{l_prefix}/share/doc >/dev/null 2>&1 || true

    #   determine installation files
    %{l_rpmtool} files -v -ofiles -r$RPM_BUILD_ROOT %{l_files_std}

%files -f files

%clean

