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

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

Name:           libgpr2
Version:        %{upstream_version}
Release:        %autorelease
Summary:        The GNAT project manager library

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] Link all tools dynamically.
Patch:          %{name}-link-tools-dynamically.patch
# [Fedora-specific] Set the library so version.
Patch:          %{name}-set-library-so-version.patch
# [Fedora-specific] Customize testing for Fedora.
Patch:          %{name}-adapt-tests-for-fedora.patch

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:  gprconfig-kb
# A gnatcoll-core that contains gnatcoll_core.gpr is needed.
BuildRequires:  gnatcoll-core-devel >= 25.0.0
BuildRequires:  gnatcoll-gmp-devel
BuildRequires:  gnatcoll-iconv-devel
BuildRequires:  xmlada-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}

# Cannot be installed with the "next" version of libgpr2.
Conflicts:      libgpr2_next

%global common_description_en \
An Ada library for handling GNAT project files.

%description %{common_description_en}


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

%package devel
Summary:        Development files for the GNAT project manager library
Requires:       %{name}%{?_isa} = %{version}-%{release}
Requires:       fedora-gnat-project-common
Requires:       libgpr-devel
Requires:       gnatcoll-core-devel
Requires:       gnatcoll-gmp-devel
Requires:       gnatcoll-iconv-devel
Requires:       xmlada-devel

%description devel %{common_description_en}

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


%package tools
Summary:        Tools based on the GNAT project manager library
License:        GPL-3.0-or-later
Requires:       %{name}%{?_isa} = %{version}-%{release}
Requires:       fedora-gnat-project-common

%description tools %{common_description_en}

This package contains tools to manage and build GNAT projects. Most of these
tools are rewrites of those provided in package gprbuild.


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

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

# Use 'gnatcoll_core.gpr' instead of 'gnatcoll.gpr' in the testsuite. We use
# sed because patching fails for some reason (probably because of the DOS line
# endings used throughout the test suite.
find ./testsuite -type f -name "*.gpr" \
    | xargs sed -i -e "s,\"gnatcoll\",\"gnatcoll_core\","

# Update some release specific information in the source code. The substitutions
# are scoped to specific lines to increase the chance of detecting code changes
# at this point. Sed should exit with exit code 0 if the substitution succeeded
# (using `t`, jump to end of script) or exit with a non-zero exit code if the
# substitution failed (using `q1`, quit with exit code 1).
sed --in-place \
    --expression='11 { s,18.0w,%{upstream_version},         ; t; q1 }' \
    --expression='14 { s,19940713,%{upstream_release_date}, ; t; q1 }' \
    --expression='16 { s,"2016",Date (1 .. 4),              ; t; q1 }' \
    --expression='21 { s,Gnatpro,GPL,                       ; t; q1 }' \
    src/lib/gpr2-version.ads

# Initialize some variables.
make LIBGPR2_TYPES='relocatable' PYTHON='%{python3}' \
     GPR2KBDIR='%{_datadir}/gprconfig' FORCE_PARSER_GEN=force \
     setup

# Scenario variables for GRPbuild project.
%global scn_vars %{shrink:
    -XVERSION=%{version}
    -XGPR2_BUILD=release
    -XLIBRARY_TYPE=relocatable
    -XXMLADA_BUILD=relocatable
}


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

%build

export VERSION=%{version}

# Build the library and tools.
%{make_build} GPRBUILD_OPTIONS='%{GPRbuild_flags} %{scn_vars}' \
              build-lib-relocatable
%{make_build} GPRBUILD_OPTIONS='%{GPRbuild_flags} %{scn_vars} -cargs -fPIE -gargs' \
              build-tools


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

%install

# Install the library and tools.
%{GPRinstall} %{scn_vars} --no-build-var -P gpr2.gpr
%{GPRinstall} %{scn_vars} --mode=usage   -P tools/gpr2-tools.gpr

# Fix up the symbolic link to the shared library.
ln --symbolic --force %{name}.so.%{version} %{buildroot}%{_libdir}/%{name}.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

# Ensure all tools start with `gpr2' to prevent conflicts and make a clear
# distinction with the tools of gprbuild.
for f in gprconfig gprdoc gprinspect; do
    mv %{buildroot}%{_bindir}/$f %{buildroot}%{_bindir}/${f/gpr/gpr2}
done

# 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=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}/gpr2.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: Replace the value of Source_Dirs with a pathname based on
#    Directories.Includedir.
# 4: Replace the value of Library_Dir with Directories.Libdir.
# 5: Replace the value of Library_ALI_Dir with a pathname based on
#    Directories.Libdir.

find %{buildroot} -exec stat --format "%A %n" {} \;
ls -l %{buildroot}%{_libdir}


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

%if %{with check}
%check

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

# Run the tests. A failing test will, for now, allow the package build
# to proceed. Tests fail for various reasons, some of which are hard
# to fix by the maintainer (e.g., compiler bugs, resource limitations).
%python3 testsuite/testsuite.py \
         --show-error-output \
         --max-consecutive-failures=4 \
         --failure-exit-code=0

%endif


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

%files
%license LICENSE-lib
%doc README*
%{_libdir}/%{name}.so.%{version}


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


%files tools
%license LICENSE-tool
%{_bindir}/gpr2build
%{_bindir}/gpr2clean
%{_bindir}/gpr2config
%{_bindir}/gpr2doc
%{_bindir}/gpr2inspect
%{_bindir}/processes-wrapper


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

%changelog
%autochangelog