# fedora specific; TODO: merge with altlinux
%__global_ldflags       -Wl,-z,relro
# These RPM macros are used in packages containing Ada code.
# Copyright 2009 - 2024 B. Persson, Bjorn@Rombobeorn.se
# Copying and distribution of this file, with or without modification, are
# permitted in any medium without royalty provided the copyright notice and
# this notice are preserved. This file is offered as-is, without any warranty.

%_GNAT_project_dir /usr/share/gpr
# _GNAT_project_dir is the directory where GNAT project files shall be.

%GNAT_builder_concurrency_flags %{?_smp_build_ncpus:-j%{_smp_build_ncpus}}
# GNAT_builder_concurrency_flags controls how many compilations the builder
# runs in parallel.
# Gnatmake and GPRbuild have a -j parameter just like Make, but they might not
# understand any other Make parameter that might appear in _smp_mflags some
# day, so we take the value from _smp_build_ncpus instead.

%GNAT_builder_rpath_flags %{?!GNAT_add_rpath:-R}
# GNAT_builder_rpath_flags controls the inclusion of a runtime library search
# path (runpath) in the built binaries.
# By default -R is passed to avoid an unnecessary runpath. The builder's
# computed runpath can be enabled by defining GNAT_add_rpath.

# Note: Gnatmake currently ignores -R when it builds shared libraries, and
# always adds a runpath. Libraries need to be post-processed with chrpath, or
# GPRbuild can be used instead.

%GNAT_builder_flags %{shrink:
    %{GNAT_builder_concurrency_flags} %{GNAT_builder_rpath_flags} -p -vl
    -XHARDWARE_PLATFORM=%{_arch}
}
# GNAT_builder_flags contains only parameters for the builder, that is either
# Gnatmake or GPRbuild. It does not include flags for the tools that the builder
# invokes.
# -p creates directories specified in project files.
# -vl shows the command lines for tools that the builder invokes, making build
# logs more useful.
# HARDWARE_PLATFORM is used by directories.gpr.

%build_adaflags %{shrink:
    %{__build_flags_common}
    -gnatn -gnat-p -gnatVd -gnatwn -gnatyN -Wtrampolines
}
# build_adaflags contains parameters to be passed to the compiler, GCC, when the
# language to be compiled is Ada.
# -gnatn enables pragma Inline.
# -gnat-p and -gnatVd try to prevent dangerous suppression of important checks.
# -gnatwn means don't treat warnings as errors (and also don't suppress them).
# (Otherwise -gnatwe could turn quite harmless conditions into build failures.)
# -gnatyN turns off style checks. They are irrelevant to packagers.
# -Wtrampolines turns on a warning when the compiler needs to generate a
# trampoline – executable code generated at run time on the stack. It should
# help with diagnosing build failures caused by Fedora's restrictions on
# executable stacks.

%Gnatlink_flags %{build_ldflags} -g
# -g makes Gnatlink refrain from deleting binder files so that they can be
# included in debuginfo packages.

%Gnatmake_flags %{shrink:
    -cargs %{build_adaflags}
    -largs %{Gnatlink_flags}
    -margs %{GNAT_builder_flags}
}
%GPRbuild_flags %{shrink:
    -cargs:Ada %{build_adaflags}
    -cargs:C %{build_cflags}
    -cargs:C++ %{build_cxxflags}
    -cargs:Fortran %{build_fflags}
    -largs %{build_ldflags}
    -gargs %{GNAT_builder_flags}
}
# Gnatmake_flags and GPRbuild_flags contain both the parameters for the
# builder itself and parameters that the builder shall pass to the tools it
# invokes. -margs and -gargs must be used after all other mode switches to
# restore the mode, so that other parameters will work as intended when these
# macros are included in longer command lines.

%Comfignat_make %{shrink:
    %{__make} GNAT_BUILDER_FLAGS="${GNAT_BUILDER_FLAGS:-%{GNAT_builder_flags}}"
              ADAFLAGS="${ADAFLAGS:-%{build_adaflags}}"
              CFLAGS="${CFLAGS:-%{build_cflags}}"
              CXXFLAGS="${CXXFLAGS:-%{build_cxxflags}}"
              FFLAGS="${FFLAGS:-%{build_fflags}}"
              LDFLAGS="${LDFLAGS:-%{build_ldflags}}"
              GNATLINKFLAGS="${GNATLINKFLAGS:--g}"
              dirgpr=directories.gpr
              prefix=%{_prefix}
              exec_prefix=%{_exec_prefix}
              datarootdir=%{_datarootdir}
              localstatedir=%{_localstatedir}
              datadir=%{_datadir}
              sysconfdir=%{_sysconfdir}
              gprdir=%{_GNAT_project_dir}
              mandir=%{_mandir}
              infodir=%{_infodir}
}
# Comfignat_make is the command for building a package whose build system uses
# Comfignat. It passes the standard parameters for the build tools, the
# directories project and the installation pathnames to Make. A goal for Make
# may be appended on the command line.

%GPRinstall_flags(d:s:a:) %{shrink:
    %define destdir        %{-d:%{-d*}}%{!-d:%{buildroot}}
    %define sources_subdir %{-s:%{-s*}}%{!-s:%{name}}
    %define ali_subdir     %{-a:%{-a*}}%{!-a:%{name}}
    %define relative() %(%{shrink:
        realpath --canonicalize-missing
                 --no-symlinks
                 --relative-to=%{shescape:%{_prefix}}
                 %{shescape:%{1}}
    })
    --create-missing-dirs
    --no-manifest
    --prefix=%{destdir}%{_prefix}
    --sources-subdir=%{relative:%{_includedir}}/%{sources_subdir}
    --project-subdir=%{relative:%{_GNAT_project_dir}}
    --exec-subdir=%{relative:%{_bindir}}
    --ali-subdir=%{relative:%{_libdir}}/%{ali_subdir}
    --lib-subdir=%{relative:%{_libdir}}
    --link-lib-subdir=%{relative:%{_libdir}}
}
# GPRinstall_flags contains parameters for GPRinstall that specify the standard
# pathnames for programs and libraries in Fedora. It is for use by packages that
# use GPRinstall to copy files and generate a usage project file. (Note that
# project files generated by GPRinstall don't use directories.gpr. They are
# therefore architecture-specific and don't support multilib.)
#
# Parameters:
#   -d <pathname>
#       staging directory where the directory tree shall be installed; defaults
#       to the buildroot
#   -s <pathname>
#       subdirectory under _includedir where source files shall be installed;
#       defaults to the name of the source package
#   -a <pathname>
#       subdirectory under _libdir where ALI files shall be installed; defaults
#       to the name of the source package
#
# Strings passed as macro parameters will be inserted unchanged in the expanded
# text. Thus shell syntax may be used in the parameters, and will be processed
# by the shell after the macro expansion.
#
# If destdir is a relative pathname, then it can't be prepended to the *-subdir
# parameters, because then GPRinstall would interpret destdir as relative to
# _prefix. All attempts to make the macro convert destdir into an absolute
# pathname break one usecase or another. Instead realpath is used to make all
# the _*dir pathnames relative to _prefix so that only --prefix needs destdir
# prepended.
#
# It would have been good to include -m to install only the interface files
# instead of all the source files – but it has a bug that makes libraries
# unusable, several Adacore libraries contain filenames that trigger the bug,
# and Adacore is not in any hurry to fix the bug:
# https://github.com/AdaCore/gprbuild/issues/138

%GPRinstall(d:s:a:) gprinstall %{GPRinstall_flags %{**}}
# This macro is the basis of a command to run GPRinstall. It is for use in spec
# files that invoke GPRinstall directly without involving Make or similar. The
# macro parameters are the same as for GPRinstall_flags. Append other parameters
# to GPRinstall on the command line.

# These deprecated macro names are retained for compatibility:
%GPRbuild_optflags %{GPRbuild_flags}
%Gnatmake_optflags %{Gnatmake_flags}
%GNAT_optflags %{Gnatmake_flags}
