File ghc-cereal.spec of Package ghc-cereal

#
# spec file for package ghc-cereal
#
# Copyright (c) 2023 SUSE LLC
#
# All modifications and additions to the file contributed by third parties
# remain the property of their copyright owners, unless otherwise agreed
# upon. The license for this file, and modifications and additions to the
# file, is the same license as for the pristine package itself (unless the
# license for the pristine package is not an Open Source License, in which
# case the license is the MIT License). An "Open Source License" is a
# license that conforms to the Open Source Definition (Version 1.9)
# published by the Open Source Initiative.

# Please submit bugfixes or comments via https://bugs.opensuse.org/
#


%global pkg_name cereal
%global pkgver %{pkg_name}-%{version}
%bcond_with tests
%global ghc_options -fhide-source-paths
Name:           ghc-%{pkg_name}
Version:        0.5.8.3
Release:        0
Summary:        A binary serialization library
License:        BSD-3-Clause
URL:            https://hackage.haskell.org/package/%{pkg_name}
Source0:        https://hackage.haskell.org/package/%{pkg_name}-%{version}/%{pkg_name}-%{version}.tar.gz
BuildRequires:  ghc-Cabal-devel
BuildRequires:  ghc-array-devel
BuildRequires:  ghc-array-prof
BuildRequires:  ghc-base-devel
BuildRequires:  ghc-base-prof
BuildRequires:  ghc-bytestring-devel
BuildRequires:  ghc-bytestring-prof
BuildRequires:  ghc-containers-devel
BuildRequires:  ghc-containers-prof
BuildRequires:  ghc-rpm-macros
ExcludeArch:    %{ix86}
%if %{with tests}
BuildRequires:  ghc-QuickCheck-devel
BuildRequires:  ghc-QuickCheck-prof
BuildRequires:  ghc-test-framework-devel
BuildRequires:  ghc-test-framework-prof
BuildRequires:  ghc-test-framework-quickcheck2-devel
BuildRequires:  ghc-test-framework-quickcheck2-prof
%endif

%description
A binary serialization library, similar to binary, that introduces an isolate
primitive for parser isolation, and labeled blocks for better error messages.

%package devel
Summary:        Haskell %{pkg_name} library development files
Requires:       %{name} = %{version}-%{release}
Requires:       ghc-compiler = %{ghc_version}
Requires(post): ghc-compiler = %{ghc_version}
Requires(postun): ghc-compiler = %{ghc_version}

%description devel
This package provides the Haskell %{pkg_name} library development files.

%package -n ghc-%{pkg_name}-doc
Summary:        Haskell %{pkg_name} library documentation
Requires:       ghc-filesystem
BuildArch:      noarch

%description -n ghc-%{pkg_name}-doc
This package provides the Haskell %{pkg_name} library documentation.

%package -n ghc-%{pkg_name}-prof
Summary:        Haskell %{pkg_name} profiling library
Requires:       ghc-%{pkg_name}-devel = %{version}-%{release}
Supplements:    (ghc-%{pkg_name}-devel and ghc-prof)

%description -n ghc-%{pkg_name}-prof
This package provides the Haskell %{pkg_name} profiling library.

%prep
%autosetup -n %{pkg_name}-%{version}

%build
# Create a small ghc wrapper to filter unsupported GHC flags (e.g. -fobject-determinism)
# This is needed because the build environment may inject flags not recognised by
# the system ghc (causing "unrecognised flag" errors). Keep the wrapper minimal.
mkdir -p tools
cat > tools/ghc <<'EOF'
#!/bin/bash
set -e
# Filter out unsupported GHC flags such as -fobject-determinism.
# This wrapper also behaves correctly if invoked as ghc-<version>.
filter_token="-fobject-determinism"
# determine target ghc binary based on invocation name, falling back to /usr/bin/ghc
name="$(basename "$0")"
if [ -x "/usr/bin/$name" ]; then
  target="/usr/bin/$name"
elif [ -x "/usr/bin/ghc" ]; then
  target="/usr/bin/ghc"
else
  # fallback to /usr/bin/ghc even if not executable; let exec report error
  target="/usr/bin/ghc"
fi

args=()
for a in "$@"; do
  if [[ "$a" == --ghc-options=* ]]; then
    prefix="--ghc-options="
    val="${a#${prefix}}"
    new=""
    for w in $val; do
      if [ "$w" = "$filter_token" ]; then
        continue
      fi
      if [ -z "$new" ]; then
        new="$w"
      else
        new="$new $w"
      fi
    done
    if [ -n "$new" ]; then
      args+=( "${prefix}${new}" )
    fi
    continue
  fi
  if [ "$a" = "$filter_token" ]; then
    continue
  fi
  if [[ "$a" == *"$filter_token"* ]]; then
    tmp="${a//$filter_token/}"
    tmp="$(echo "$tmp" | tr -s ' ')"
    tmp="$(echo "$tmp" | sed -e 's/^ *//' -e 's/ *$//')"
    if [ -n "$tmp" ]; then
      args+=( "$tmp" )
    fi
    continue
  fi
  args+=( "$a" )
done
exec "$target" "${args[@]}"
EOF
chmod +x tools/ghc
# also create ghc-<version> wrapper name; Cabal may invoke ghc-<version>
ln -s ghc tools/ghc-9.10.2 || true
export PATH="$(pwd)/tools:$PATH"

# Custom build: avoid passing unsupported GHC flags such as -fobject-determinism
# Compile Setup if present
if [ -f Setup.hs -o -f Setup.lhs ]; then
  PATH="$(pwd)/tools:$PATH" ghc --make -package Cabal -no-user-package-db -dynamic Setup
  # If Setup was created, replace it with a wrapper that filters -fobject-determinism
  if [ -x Setup ]; then
    mv Setup Setup.real || true
    cat > Setup <<'EOF'
#!/bin/bash
# Wrapper for Setup to remove unsupported -fobject-determinism from arguments
filter_token="-fobject-determinism"
args=()
for a in "$@"; do
  if [[ "$a" == --ghc-options=* ]]; then
    prefix="--ghc-options="
    val="${a#${prefix}}"
    new=""
    for w in $val; do
      if [ "$w" = "$filter_token" ]; then
        continue
      fi
      if [ -z "$new" ]; then
        new="$w"
      else
        new="$new $w"
      fi
    done
    if [ -n "$new" ]; then
      args+=( "${prefix}${new}" )
    fi
  else
    if [ "$a" = "$filter_token" ]; then
      continue
    fi
    if [[ "$a" == *"$filter_token"* ]]; then
      tmp="${a//$filter_token/}"
      tmp="$(echo "$tmp" | tr -s ' ')"
      tmp="$(echo "$tmp" | sed -e 's/^ *//' -e 's/ *$//')"
      if [ -n "$tmp" ]; then
        args+=( "$tmp" )
      fi
    else
      args+=( "$a" )
    fi
  fi
done
exec "$(pwd)/Setup.real" "${args[@]}"
EOF
    chmod +x Setup
  fi
fi
LANG=C.utf8 PATH="$(pwd)/tools:$PATH" ./Setup configure --prefix=/usr --libdir=/usr/lib64 --docdir=/usr/share/licenses/ghc-cereal '\''--libsubdir=$compiler/lib/$pkgid'\'' '\''--datasubdir=$pkgid'\'' '\''--libexecsubdir=$pkgid'\'' --ghc --dynlibdir=/usr/lib64/ghc-9.10.2/lib --global '\''--ghc-options=-fhide-source-paths'\'' -v --enable-library-profiling --enable-shared --htmldir=/usr/share/doc/packages/ghc-9.10.2/html/libraries/cereal-0.5.8.3 --global
LANG=C.utf8 PATH="$(pwd)/tools:$PATH" ./Setup build

%install
%ghc_lib_install

%check
%cabal_test

%post devel
%ghc_pkg_recache

%postun devel
%ghc_pkg_recache

%files -f %{name}.files
%license LICENSE

%files devel -f %{name}-devel.files
%doc CHANGELOG.md

%files -n ghc-%{pkg_name}-doc -f ghc-%{pkg_name}-doc.files
%license LICENSE

%files -n ghc-%{pkg_name}-prof -f ghc-%{pkg_name}-prof.files

%changelog
openSUSE Build Service is sponsored by