# Per-platform rpm configuration file for openmamba.

#==============================================================================
# ---- openmamba specific macros

%_dist mamba
%_docdir            %_defaultdocdir
%_topdir            %{_usr}/src/RPM

%majorminor         %(echo %version | cut -d. -f 1-2)
%MAJver             %(echo %version | cut -d. -f 1)
%majver             %majorminor
%maj3ver            %(echo %version | cut -d. -f 1-3)

#==============================================================================
# ---- configure macros.  note that most of these are inherited
#      from the defaults.

# GCC toolchain
%__cc_gcc gcc
%__cxx_gcc g++
%__cpp_gcc gcc -E

# Clang toolchain
%__cc_clang clang
%__cxx_clang clang++
%__cpp_clang clang-cpp

# Default to the GCC toolchain
%toolchain gcc

%__cc %{expand:%%{__cc_%{toolchain}}}
%__cxx %{expand:%%{__cxx_%{toolchain}}}
%__cpp %{expand:%%{__cpp_%{toolchain}}}

# Compiler macros to use for invoking compilers in spec files for packages that
# want to use the default compiler and don't care which compiler that is.
%build_cc %{__cc}
%build_cxx %{__cxx}
%build_cpp %{__cpp}

#==============================================================================
# --- Compiler flags control.
#
# Please consult buildflags.md for parts that can be configured
# from RPM spec files.

# Use the remove-section option to force the find-debuginfo script
# to move the annobin notes into the separate debuginfo file.
%_find_debuginfo_extra_opts %{?_annotated_build:--remove-section .gnu.build.attributes}

# Include frame pointer information by default
# Use "%undefine _include_frame_pointers" to disable.
%_include_frame_pointers %{undefined rhel} || 0%{?rhel} >= 11
%_frame_pointers_cflags %{expr:0%{?_include_frame_pointers} ? "-fno-omit-frame-pointer" : ""}
%_frame_pointers_cflags_x86_64 %{expr:0%{?_include_frame_pointers} ? "-mno-omit-leaf-frame-pointer" : ""}
%_frame_pointers_cflags_aarch64 %{expr:0%{?_include_frame_pointers} ? "-mno-omit-leaf-frame-pointer" : ""}
%_frame_pointers_cflags_s390x %{expr:0%{?_include_frame_pointers} ? "-mbackchain" : ""}

# Fail linking if there are undefined symbols.  Required for proper
# ELF symbol versioning support.  Disabled by default.
# Use "%define _ld_strict_symbol_defs 1" to enable.
#%_ld_strict_symbol_defs		1
%_ld_symbols_flags		%{?_ld_strict_symbol_defs:-Wl,-z,defs}

# use "%undefine _ld_as_needed" to disable.
%_ld_as_needed		1
%_ld_as_needed_flags	%{?_ld_as_needed:-Wl,--as-needed}

# s390x currently does not support packed relocations.
%_ld_pack_relocs %[ "%{_arch}" == "x86_64" || "%{_arch}" == "i386" || "%{_arch}" == "ppc64le" || "%{_arch}" == "aarch64"]
%_ld_pack_relocs_flags %[0%{?_ld_pack_relocs} ? "-Wl,-z,pack-relative-relocs" : ""]

# LTO is the default in openmamba.
#   "%define _lto_cflags %{nil}"  to opt out
#
# We currently have -ffat-lto-objects turned on out of an abundance of
# caution.  To remove it we need to do a check of the installed .o/.a files
# to verify they have real sections/symbols after LTO stripping.  That
# way we can detect installing an unusable .o/.a file.  This is on the TODO
# list for F34.
%_gcc_lto_cflags	-flto=auto -ffat-lto-objects
%_clang_lto_cflags	-flto=thin -ffat-lto-objects
%_lto_cflags		%{expand:%%{_%{toolchain}_lto_cflags}}

# Default fortification level.
#   "%define _fortify_level 2" to downgrade and
#   "%define _fortify_level 0" or "%undefine _fortify_level" to disable
#
# We use a single -Wp here to enforce order so that ccache does not ever
# reorder them.
%_fortify_level		3
%_fortify_level_flags	%[ 0%{?_fortify_level} > 0 ? "-Wp,-U_FORTIFY_SOURCE,-D_FORTIFY_SOURCE=%{_fortify_level}" : "" ]

# This can be set to a positive integer to obtain increasing type
# safety levels for C.  See buildflags.md.
%build_type_safety_c 3

# Some linkers default to a build-id algorithm that is not supported by rpmbuild,
# so we need to specify the right algorithm to use.
%_build_id_flags	-Wl,--build-id=sha1

%_general_options       -O2 %{?_lto_cflags} -fexceptions -g -grecord-gcc-switches -pipe
%_warning_options       -Wall%[%__build_for_lang_any && "%toolchain" == "gcc" ? " -Wno-complain-wrong-lang" : ""]%[%__build_for_lang_c + %__build_for_lang_cxx ? " -Wformat -Werror=format-security" : ""]%[%__build_for_lang_c && (%build_type_safety_c == 0) ? " -fpermissive" : ""]%[%__build_for_lang_c && (%build_type_safety_c == 1) ? " -Wno-error=int-conversion" : ""]%[%__build_for_lang_c && (%build_type_safety_c > 0 && %build_type_safety_c < 3) ? " -Wno-error=incompatible-pointer-types" : ""]
%_preprocessor_defines  %{_fortify_level_flags} -Wp,-D_GLIBCXX_ASSERTIONS

# Common variables are no longer generated by default by gcc and clang
# If they are needed then add "%define _legacy_common_support 1" to the spec file.
%_legacy_options	%{?_legacy_common_support: -fcommon}

%__global_compiler_flags %{_general_options} %{_warning_options} %{_preprocessor_defines} %{_legacy_options}

# Internal macros. Do not use directly.  These variables can be rebound
# to suppress certain frontend-specific compiler flags (or in the case
# of __build_for_lang_any, frontend-agnostic flags).  Dynamic scoping
# and shadowing redefinitions are used for the __build_for_* variables
# to remain largely compatible with existing spec files that have
# hard-coded assumptions which macros assume which other macros.
# The __build_flags_no_macro_warning construct suppresses a warning
# about unused RPM macros.
%__build_for_lang_c 1
%__build_for_lang_cxx 1
%__build_for_lang_any 1
%__build_flags_no_macro_warning %[%__build_for_lang_c + %__build_for_lang_cxx + %__build_for_lang_any ? "" : ""]
%__build_flags_common() %{expand:%define __build_for_lang_c 0}%{expand:%define __build_for_lang_cxx 0}%{expand:%define __build_for_lang_any 0}%{__build_flags_no_macro_warning}%{optflags}
%__build_flags_lang_c() %{expand:%define __build_for_lang_cxx 0}%{expand:%define __build_for_lang_any 0}%{__build_flags_no_macro_warning}%{optflags}
%__build_flags_lang_cxx() %{expand:%define __build_for_lang_c 0}%{expand:%define __build_for_lang_any 0}%{__build_flags_no_macro_warning}%{optflags}

#------------------------------------------------------------------------
# Macros for building and installing

%make_jobs %{__make} %{?_smp_mflags}

%make \
  CFLAGS="${CFLAGS:-%optflags}" ; export CFLAGS ; \
  CXXFLAGS="${CXXFLAGS:-%optflags}" ; export CXXFLAGS ; \
  FFLAGS="${FFLAGS:-%optflags}" ; export FFLAGS ; \
  %{make_jobs}

%makeinstall \
  make DESTDIR=%{buildroot} install

%makeoldinstall \
  make \\\
        prefix=%{?buildroot:%{buildroot}}%{_prefix} \\\
        exec_prefix=%{?buildroot:%{buildroot}}%{_exec_prefix} \\\
        bindir=%{?buildroot:%{buildroot}}%{_bindir} \\\
        sbindir=%{?buildroot:%{buildroot}}%{_sbindir} \\\
        sysconfdir=%{?buildroot:%{buildroot}}%{_sysconfdir} \\\
        datadir=%{?buildroot:%{buildroot}}%{_datadir} \\\
        includedir=%{?buildroot:%{buildroot}}%{_includedir} \\\
        libdir=%{?buildroot:%{buildroot}}%{_libdir} \\\
        libexecdir=%{?buildroot:%{buildroot}}%{_libexecdir} \\\
        localstatedir=%{?buildroot:%{buildroot}}%{_localstatedir} \\\
        sharedstatedir=%{?buildroot:%{buildroot}}%{_sharedstatedir} \\\
        mandir=%{?buildroot:%{buildroot}}%{_mandir} \\\
        infodir=%{?buildroot:%{buildroot}}%{_infodir} \\\
  install

%makeinstall_perl make pure_install DESTDIR=%{buildroot}

#------------------------------------------------------------------------
# Macros for installing and uninstalling info pages

%__install_info       /usr/bin/install-info

%install_info() \
   %{__install_info} %{_infodir}/%{1}.gz %{_infodir}/dir ||:\;

%uninstall_info() \
if [ "$1" = "0" ]; then \
   %{__install_info} --delete %{_infodir}/%{1}.gz %{_infodir}/dir \
fi \
%{nil}

#---------------------------------------------------------------------
#	Expanded at end of %install scriptlet.
#

%__arch_install_post   %{nil}

# Standard brp-macro naming:
# convert all '-' in basename to '_', add two leading underscores.
%__brp_compress %{_rpmconfigdir}/brp-compress %{?_prefix}
%__brp_strip %{_rpmconfigdir}/brp-strip %{__strip}
%__brp_strip_lto %{_rpmconfigdir}/openmamba/brp-strip-lto %{__strip}
%__brp_strip_comment_note %{_rpmconfigdir}/brp-strip-comment-note %{__strip} %{__objdump}
%__brp_strip_static_archive %{_rpmconfigdir}/brp-strip-static-archive %{__strip}
%__brp_remove_la_files %{_rpmconfigdir}/brp-remove-la-files

%__os_install_post    \
    %{?__brp_compress} \
    %{?__brp_strip} \
    %{?__brp_strip_lto} \
    %{?__brp_strip_static_archive} \
    %{?__brp_strip_comment_note} \
    %{?__brp_remove_la_files} \
%{nil}

%__spec_install_post\
    %{?__debug_package:%{__debug_install_post}}\
    %{__arch_install_post}\
    %{__os_install_post}\
%{nil}

