File ghc-hpke.spec of Package ghc-hpke

#
# spec file for package ghc-hpke
#
# Copyright (c) 2025 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 hpke
%global pkgver %{pkg_name}-%{version}
%bcond_with tests
%global ghc_options -fhide-source-paths
%global ghc_version 9.10.2
Name:           ghc-%{pkg_name}
Version:        0.0.0
Release:        1.1
Summary:        Hybrid Public Key Encryption
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-base-devel
BuildRequires:  ghc-base-prof
BuildRequires:  ghc-base16-bytestring-devel
BuildRequires:  ghc-base16-bytestring-prof
BuildRequires:  ghc-bytestring-devel
BuildRequires:  ghc-bytestring-prof
BuildRequires:  ghc-crypton-devel
BuildRequires:  ghc-crypton-prof
BuildRequires:  ghc-memory-devel
BuildRequires:  ghc-memory-prof
BuildRequires:  ghc-rpm-macros
ExcludeArch:    %{ix86}
%if %{with tests}
BuildRequires:  ghc-QuickCheck-devel
BuildRequires:  ghc-QuickCheck-prof
BuildRequires:  ghc-hspec-devel
BuildRequires:  ghc-hspec-prof
%endif

%description
Hybrid Public Key Encryption defined in RFC9180.

%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
# Use explicit build steps instead of the %ghc_lib_build macro so we can
# create a wrapper Setup script that strips unsupported ghc flags
# (e.g. -fobject-determinism) before any configure/build invocation.
cd hpke-%{version}

# Create a small ghc wrapper in the build directory that filters out
# unsupported flags (notably -fobject-determinism). Prepend this
# directory to PATH so any ghc invocation during ./Setup will use it.
real_ghc="$(command -v ghc || echo /usr/bin/ghc)"
mkdir -p _build_bin
cat > _build_bin/ghc <<'__GHC_WRAPPER__'
#!/bin/sh
# Filter out -fobject-determinism occurrences from ghc arguments.
args=()
for a in "$@"; do
  case "$a" in
    -fobject-determinism)
      # drop this arg
      ;;
    *-fobject-determinism*)
      # remove the substring and keep the rest
      new="$(printf "%s" "$a" | sed 's/-fobject-determinism//g' | sed -e 's/  */ /g' -e 's/^ //' -e 's/ $//')"
      if [ -n "$new" ]; then
        args+=("$new")
      fi
      ;;
    *)
      args+=("$a")
      ;;
  esac
done
# Execute the real ghc with filtered arguments
exec "$real_ghc" "${args[@]}"
__GHC_WRAPPER__
chmod +x _build_bin/ghc
PATH="$(pwd)/_build_bin:$PATH"

cp -f /usr/share/ghc-rpm-macros/Setup.hs .
# Compile Setup to a real binary and move it to Setup.real
if [ -f Setup.hs ] || [ -f Setup.lhs ]; then
  ghc --make -package Cabal -no-user-package-db -dynamic Setup -o Setup.real || true
fi
# Create wrapper script named Setup that strips -fobject-determinism
cat > Setup <<'__EOF__'
#!/bin/sh
# Robust wrapper to strip '-fobject-determinism' from any --ghc-options
# argument, handling the forms:
#  --ghc-options=...
#  --ghc-options "..."
# and when passed after other flags like --global '--ghc-options=...'.
newargs=""
escape_for_single_quote() {
  # escape single quotes for safe single-quote wrapping
  printf "%s" "$1" | sed "s/'/'\\''/g"
}
while [ $# -gt 0 ]; do
  raw="$1"
  # remove surrounding single or double quotes if present
  arg="$(printf "%s" "$raw" | sed -e "s/^'\(.*\)'$/\1/" -e 's/^"\(.*\)"$/\1/')"
  case "$arg" in
    --ghc-options=*)
      val="${arg#--ghc-options=}"
      val2="$(printf "%s" "$val" | sed -e 's/-fobject-determinism//g' -e 's/  */ /g' -e 's/^ //' -e 's/ $//')"
      if [ -n "$val2" ]; then
        esc="$(escape_for_single_quote "$val2")"
        newargs="$newargs --ghc-options='$esc'"
      fi
      shift
      ;;
    --ghc-options)
      shift
      raw="$1"
      arg="$(printf "%s" "$raw" | sed -e "s/^'\(.*\)'$/\1/" -e 's/^"\(.*\)"$/\1/')"
      val="$arg"
      val2="$(printf "%s" "$val" | sed -e 's/-fobject-determinism//g' -e 's/  */ /g' -e 's/^ //' -e 's/ $//')"
      if [ -n "$val2" ]; then
        esc="$(escape_for_single_quote "$val2")"
        newargs="$newargs --ghc-options='$esc'"
      fi
      shift
      ;;
    *)
      esc="$(escape_for_single_quote "$arg")"
      newargs="$newargs '$esc'"
      shift
      ;;
  esac
done
# Prefer runhaskell over compiled Setup.real to ensure any ghc run is
# executed through the PATH where we injected our ghc wrapper. If
# runhaskell is not available, fall back to running Setup.real.
if command -v runhaskell >/dev/null 2>&1 && [ -f Setup.hs ]; then
  eval "exec runhaskell Setup.hs $newargs"
elif [ -x ./Setup.real ]; then
  eval "exec ./Setup.real $newargs"
else
  echo "No Setup.real or Setup.hs found" >&2
  exit 1
fi
__EOF__
chmod +x Setup

LANG=C.utf8
# Invoke configure without passing --ghc-options to avoid injecting
# unsupported flags (e.g. -fobject-determinism) by the build macros.
# Additionally force the Setup tool to use our ghc wrapper via --with-ghc
GHC="$(pwd)/_build_bin/ghc" PATH="$(pwd)/_build_bin:$PATH" ./Setup configure --with-ghc="$(pwd)/_build_bin/ghc" --prefix=/usr --libdir=/usr/lib64 --docdir=/usr/share/licenses/ghc-hpke \
  --ghc -v --enable-library-profiling --enable-shared
GHC="$(pwd)/_build_bin/ghc" PATH="$(pwd)/_build_bin:$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