# The test suite is normally run. It can be disabled with "--without=check".
%bcond_without check

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

Name:           prettier-ada
Version:        %{upstream_version}
Release:        %autorelease
Summary:        Port of the Prettier formatter to the Ada programming language

License:        Apache-2.0 WITH LLVM-exception

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

# [Fedora-specific] Set library soname.
Patch:          %{name}-set-soname-of-library.patch

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

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

# VSS isn't available on s390x.
ExcludeArch:    s390x

%global common_description_en \
This project is a port of the Prettier formatter to the Ada programming \
language. The aim of this project is to provide a tool that allows Ada \
programmers to create formatters for any language of their choosing. \
\
Please note that only the core generic formatting algorithm and its \
dependencies were ported. The formatters for all supported languages are not \
included in this project.

%description %{common_description_en}


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

%package devel
Summary:        Development files for Prettier-Ada
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 Prettier-Ada.


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

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

# Scenario variables for GRPbuild project.
%global scn_vars -XVERSION=%{version} \\\
                 -XLIBRARY_TYPE=relocatable \\\
                 -XPRETTIER_ADA_BUILD_MODE=prod \\\
                 -XPRETTIER_ADA_LIBRARY_TYPE=relocatable

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

%build
gprbuild %{GPRbuild_flags} %{scn_vars} -P prettier_ada.gpr


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

%install
%{GPRinstall} %{scn_vars} -P prettier_ada.gpr

# Fix up the symbolic link to the shared library.
ln --symbolic --force libprettier_ada.so.%{version} %{buildroot}%{_libdir}/libprettier_ada.so

# Before making the project files architecture-independent, copy the buildroot
# into a separate directory for later testing. The testsuite fails if applied to
# the buildroot after making the project files architecture-independent because
# of the hardcoded paths in `directories.gpr`.
%if %{with check}
%global checkroot %{_builddir}/%{upstream_name}-%{upstream_version}/checkroot
mkdir %{checkroot}
cp --recursive %{buildroot}/* %{checkroot}/
%endif

# Make the generated usage project file architecture-independent.
sed --regexp-extended --in-place \
    '--expression=1i with "directories";' \
    '--expression=/^--  This project has been generated/d' \
    '--expression=/package Linker is/,/end Linker/d' \
    '--expression=s|^( *for +Source_Dirs +use +).*;$|\1(Directories.Includedir \& "/'%{name}'");|i' \
    '--expression=s|^( *for +Library_Dir +use +).*;$|\1Directories.Libdir;|i' \
    '--expression=s|^( *for +Library_ALI_Dir +use +).*;$|\1Directories.Libdir \& "/'%{name}'";|i' \
    %{buildroot}%{_GNAT_project_dir}/prettier_ada.gpr
# The Sed commands are:
# 1: Insert a with clause before the first line to import the directories
#    project.
# 2: Delete a comment that mentions the architecture.
# 3: Delete the package Linker, which contains linker parameters that a
#    shared library normally doesn't need, and can contain architecture-
#    specific pathnames.
# 4: Replace the value of Source_Dirs with a pathname based on
#    Directories.Includedir.
# 5: Replace the value of Library_Dir with Directories.Libdir.
# 6: Replace the value of Library_ALI_Dir with a pathname based on
#    Directories.Libdir.


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

%if %{with check}
%check

# Build the test programs.
gprbuild %{GPRbuild_flags} %{scn_vars} \
         -P testsuite/test_programs/test_programs.gpr

# Make the files of this packages visible to the test runner.
export PATH=%{checkroot}%{_bindir}:${PWD}/testsuite/test_programs/bin:$PATH
export LD_LIBRARY_PATH=%{checkroot}%{_libdir}:$LD_LIBRARY_PATH
export GPR_PROJECT_PATH=%{checkroot}%{_GNAT_project_dir}:$GPR_PROJECT_PATH

# Run the tests.
%python3 testsuite/testsuite.py \
         --show-error-output \
         --max-consecutive-failures=4

%endif


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

%files
%license LICENSE.txt
%doc README*
%{_libdir}/libprettier_ada.so.%{version}


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


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

%changelog
%autochangelog