# TODO: Run test suite (don't use e3?)
# TODO: Regenerate UCD file?

# The test suite isn't normally run as some of the test data must be downloaded
# separately. It can be enabled with "--with=check".
%bcond_with check

# Upstream source information.
%global upstream_owner    AdaCore
%global upstream_name     VSS
%global upstream_version  25.0.0
%global upstream_gittag   v%{upstream_version}

Name:           vss
Version:        %{upstream_version}
Release:        1%{?dist}
Summary:        High level string and text processing library

License:        Apache-2.0 WITH LLVM-Exception AND Unicode-DFS-2016
# VSS itself is licensed under Apache 2.0 with a runtime exception. The Unicode
# license is mentioned as Unicode data files were used as an input for
# generating some of VSS' source code.

URL:            https://github.com/%{upstream_owner}/%{upstream_name}
Source0:        %{url}/archive/%{upstream_gittag}/%{upstream_name}-%{upstream_version}.tar.gz

# Test data. See "data/README.md" on how to create.
#
# +--------------------------------------------------------+-------------+
# | Contents                                               | License     |
# +--------------------------------------------------------+-------------+
# | https://www.unicode.org/Public/15.1.0/ucd/UCD.zip      | Unicode-3.0 |
# | https://www.unicode.org/Public/emoji/15.1/*            | Unicode-3.0 |
# | https://github.com/nigeltao/parse-number-fxx-test-data | Apache-2.0  |
# | https://github.com/json5/json5-tests.git               | MIT         |
# +--------------------------------------------------------+-------------+
#
%if %{with check}
# Open-issue: Source file cannot be found during build when using
# --with=check. Works only when `bcond_without check`.
Source1:        vss-tests-data.tar.bz2
%endif

BuildRequires:  gcc-gnat gprbuild make sed
# A fedora-gnat-project-common that contains the new GPRinstall macro.
BuildRequires:  fedora-gnat-project-common >= 3.21
BuildRequires:  xmlada-devel
%if %{with check}
BuildRequires:  python3-devel
BuildRequires:  python3-setuptools
BuildRequires:  python3-e3-testsuite
%endif

# [Fedora-specific] Set library soname.
Patch:          %{name}-set-soname-of-libraries.patch
# [Fedora-specific] Make build options for tests more flexible.
Patch:          %{name}-build-tests-relocatable.patch

# Build only on architectures where GPRbuild is available.
ExclusiveArch:  %{GPRbuild_arches}

# The implementation of packages
#
#   - Ada.Strings.Unbounded.VSS_Aux
#       VSS source: source/text/gnat/a-suvsau.adb
#   - Ada.Strings.Wide_Wide_Unbounded.VSS_Aux
#       VSS source: source/text/gnat/a-szuvau.adb
#
# expects the unbounded string packages to implement the
# "copy-on-modification" strategy,
#
#   - Ada.Strings.Unbounded
#       GCC source: gcc/ada/libgnat/a-strunb__shared.ad[sb]
#   - Ada.Strings.Wide_Wide_Unbounded
#       GCC source: gcc/ada/libgnat/a-stzunb__shared.ad[sb]
#
# These implementations are availabe when atomic counters are available via
# package System.Atomic_Counters (GCC source: gcc/ada/libgnat/s-atocou.ads),
# which isn't the case for s390x. Hence VSS cannot build on s390x.
ExcludeArch:    s390x

%global common_description_en \
The VSS (as an abbreviation for Virtual String Subsystem) library is \
designed to provide advanced string and text processing capabilities. It \
offers a convenient and robust API that allows developers to work with \
Unicode text, regardless of its internal representation.

%description %{common_description_en}


#################
## Subpackages ##
#################

%package devel
Summary:    Development files for the VSS library
Requires:   %{name}%{?_isa} = %{version}-%{release}
Requires:   fedora-gnat-project-common

%description devel %{common_description_en}

This package contains source code and linking information for developing
applications that use the VSS library.


#############
## Prepare ##
#############

%prep
%autosetup -n %{upstream_name}-%{upstream_version} -p1

# Scenario variables for GRPbuild project.
%global scn_vars -XVERSION=%{version} \\\
                 -XVSS_BUILD_PROFILE=release \\\
                 -XVSS_LIBRARY_TYPE=relocatable \\\
                 -XVSS_MAX_SUPPORTED_INTEGER_SIZE=128 \\\
                 -XXMLADA_BUILD=relocatable

###########
## Build ##
###########

%build

# Build the library components.
for component in text gnat json regexp xml xml_templates xml_xmlada; do
    gprbuild %{GPRbuild_flags} %{scn_vars} -P gnat/vss_${component}.gpr
done;


#############
## Install ##
#############

%install

# Install the library components.
for component in text gnat json regexp xml xml_templates xml_xmlada ; do
    %{GPRinstall -s %{name}-${component} -a %{name}-${component}} \
                 %{scn_vars} --no-build-var -P gnat/vss_${component}.gpr
done

# Fix up some things that GPRinstall does wrong.
ln --symbolic --force lib%{name}.so.%{version} \
       %{buildroot}%{_libdir}/lib%{name}.so

for component in gnat json regexp xml xml-templates xml-xmlada ; do
    ln --symbolic --force lib%{name}-${component}.so.%{version} \
       %{buildroot}%{_libdir}/lib%{name}-${component}.so
done


###########
## Check ##
###########

%if %{with check}
%check

# Unpack test data.
pushd ./data
tar --extract --bzip2 --file %{SOURCE1}
popd

# Make the files installed in the buildroot visible to the testsuite.
export LD_LIBRARY_PATH=%{buildroot}%{_libdir}:$LD_LIBRARY_PATH
export GPR_PROJECT_PATH=%{buildroot}%{_GNAT_project_dir}:$GPR_PROJECT_PATH

# Build & run the tests.
make check GPRBUILD_FLAGS='%{GPRbuild_flags} %{scn_vars} -cargs -fPIE -gargs'

%endif


###########
## Files ##
###########

%files
%license LICENSE.txt
%doc README*
%{_libdir}/lib%{name}.so.%{version}
%{_libdir}/lib%{name}-gnat.so.%{version}
%{_libdir}/lib%{name}-json.so.%{version}
%{_libdir}/lib%{name}-regexp.so.%{version}
%{_libdir}/lib%{name}-xml.so.%{version}
%{_libdir}/lib%{name}-xml-templates.so.%{version}
%{_libdir}/lib%{name}-xml-xmlada.so.%{version}


%files devel
%{_GNAT_project_dir}/%{name}_text.gpr
%{_includedir}/%{name}-text
%dir %{_libdir}/%{name}-text
%attr(444,-,-) %{_libdir}/%{name}-text/*.ali
%{_libdir}/lib%{name}.so

%{_GNAT_project_dir}/%{name}_gnat.gpr
%{_includedir}/%{name}-gnat
%dir %{_libdir}/%{name}-gnat
%attr(444,-,-) %{_libdir}/%{name}-gnat/*.ali
%{_libdir}/lib%{name}-gnat.so

%{_GNAT_project_dir}/%{name}_json.gpr
%{_includedir}/%{name}-json
%dir %{_libdir}/%{name}-json
%attr(444,-,-) %{_libdir}/%{name}-json/*.ali
%{_libdir}/lib%{name}-json.so

%{_GNAT_project_dir}/%{name}_regexp.gpr
%{_includedir}/%{name}-regexp
%dir %{_libdir}/%{name}-regexp
%attr(444,-,-) %{_libdir}/%{name}-regexp/*.ali
%{_libdir}/lib%{name}-regexp.so

%{_GNAT_project_dir}/%{name}_xml.gpr
%{_includedir}/%{name}-xml
%dir %{_libdir}/%{name}-xml
%attr(444,-,-) %{_libdir}/%{name}-xml/*.ali
%{_libdir}/lib%{name}-xml.so

%{_GNAT_project_dir}/%{name}_xml_templates.gpr
%{_includedir}/%{name}-xml_templates
%dir %{_libdir}/%{name}-xml_templates
%attr(444,-,-) %{_libdir}/%{name}-xml_templates/*.ali
%{_libdir}/lib%{name}-xml-templates.so

%{_GNAT_project_dir}/%{name}_xml_xmlada.gpr
%{_includedir}/%{name}-xml_xmlada
%dir %{_libdir}/%{name}-xml_xmlada
%attr(444,-,-) %{_libdir}/%{name}-xml_xmlada/*.ali
%{_libdir}/lib%{name}-xml-xmlada.so


###############
## Changelog ##
###############

%changelog
* Sun Oct 27 2024 Dennis van Raaij <dvraaij@fedoraproject.org> - 25.0.0-1
- Updated to v25.0.0.

* Mon Jan 29 2024 Dennis van Raaij <dvraaij@fedoraproject.org> - 24.0.0-1
- New package.