%distribution OpenMandriva Lx
%vendor OpenMandriva
%bugurl https://issues.openmandriva.org
%disturl https://openmandriva.org

# Disable automatic dependencies on parent directory and symlinks for now
%_check_symlink_deps 0
%_check_dirname_deps 0

%_srcdefattr (644,root,root)

%_docdir_fmt %%{NAME}

# automatic truncation of changelogs to 3 years
%_changelog_trimtime %{lua:print(os.time() - 3 * 365 * 86400)}

# mdvbz#64914
%_rpmgio .ufdio

%__urlgetfile(url, dest) wget %1 -O %2 || (rm -f %2 && exit 1)

# For the built rpm filename format...
%disttag %{!?distsuffix:omv}%{?distsuffix}%{!?distrelease:%(sed -e 's#.*release\\ \\(\\S*\\).*#\\1#' /etc/release)}%{?distrelease}

# The output binary package file name template used when building binary packages.
# XXX: Note: escaped %% for use in headerSprintf()
%_build_name_fmt %%{ARCH}/%%{NAME}-%%{VERSION}-%%{RELEASE}-%%{DISTTAG}.%%{ARCH}.rpm

# Dynamic EVRD tags macro for use with versioned provides/requires in place of
# '%{version}-%{release}', ie. to automatically add distepoch or epoch if present
%EVRD %{?epoch:%{epoch}:}%{?version:%{version}}%{?release:-%{release}}

%rename() \
Obsoletes:	%{1} < %{?2}%{!?2:%{EVRD}} \
Provides:	%{1} = %{?2}%{!?2:%{EVRD}} \

%_default_patch_flags --reject-format=unified
%_default_patch_fuzz 0

%make \
  echo "warning: %%make is deprecated, try %%make_build instead" 1>&2\
  %{__make} %{_smp_mflags} -O

%makeinstall_std \
  echo "warning: %%makeinstall_std is deprecated, try %%make_install instead" 1>&2\
  make DESTDIR=%{?buildroot:%{buildroot}} install INSTALL="install -p"

%__lua %{_bindir}/lua
%__rc %{_bindir}/rc
%__xar %{_bindir}/xar

# mdvbz#61851
%__tar_wildcards --wildcards

%_loop_detection_loglevel 4

%_logdir %{_localstatedir}/log
%_localedir %{_datadir}/locale

%_fmoddir %{_libdir}/gfortran/modules

# use zstd to compress binary packages:
%_binary_payload w19.zstdio
%_xz_threads -1

# don't use compression for source packages.
# (It is unnecessary since the biggest part of
# a src.rpm is the source tarball -- which is
# already compressed. Compressing compressed files
# doesn't help.)
%_source_payload w.ufdio

# Use SHA-256 for FILEDIGESTS instead of default MD5
%_source_filedigest_algorithm 8
%_binary_filedigest_algorithm 8

%_duplicate_files_terminate_build 1
%_unpackaged_subdirs_terminate_build 1
%_files_listed_twice_terminate_build 1

%_build_pkgcheck_set /usr/bin/rpmlint %([ -e %{_sourcedir}/%{name}.rpmlintrc ] && echo -n --file %{_sourcedir}/%{name}.rpmlintrc)
%_build_pkgcheck_srpm /usr/bin/rpmlint %([ -e %{_sourcedir}/%{name}.rpmlintrc ] && echo -n --file %{_sourcedir}/%{name}.rpmlintrc)

# This will be enabled at some point - for now, let it pass since many
# packages aren't ready for rpmlint 2.5 yet
%_nonzero_exit_pkgcheck_terminate_build 0

%_enable_debug_packages 1

# Default extension to use (for man & info files)
%_extension .zst
%_compress zstd -f --rm -19 -T0

# Macro: %{mklibname <name> [<major> [<minor>]] [-s] [-d]}
# - %{mklibname test}               => lib64test, on a lib64 platform
# - %{mklibname test 1 -d}      => libtest1-devel
# - %{mklibname test 1 -d 0 -s} => libtest1_0-static-devel
%mklibname(ds)  %{_lib}%{1}%{!-d:%(if echo %{1} |rev |cut -b1 |grep -q '[0-9]' && [ -n "%{?2:%{2}}%{?3:_}" ]; then echo -n _; fi)}%{?2:%{2}}%{?3:_%{3}}%{-s:-static}%{-d:-devel}
%mklib32name(ds)  lib%{1}%{!-d:%(if echo %{1} |rev |cut -b1 |grep -q '[0-9]' && [ -n "%{?2:%{2}}%{?3:_}" ]; then echo -n _; fi)}%{?2:%{2}}%{?3:_%{3}}%{-s:-static}%{-d:-devel}

#==============================================================================
# ---- compiler flags.

# C compiler flags.  This is traditionally called CFLAGS in makefiles.
# Historically also available as %%{optflags}, and %%build sets the
# environment variable RPM_OPT_FLAGS to this value.
%build_cflags %{optflags}

# C++ compiler flags.  This is traditionally called CXXFLAGS in makefiles.
%build_cxxflags %{optflags}

# Fortran compiler flags.  Makefiles use both FFLAGS and FCFLAGS as
# the corresponding variable names.
%build_fflags %{optflags} -I%{_fmoddir}

# When LTOing, it is useful to duplicate CFLAGS in LDFLAGS
# See https://github.com/OpenMandrivaSoftware/rpm-openmandriva-setup/pull/3
%build_ldflags %{optflags} -Wl,-O2 %{?!_disable_ld_no_undefined: -Wl,--no-undefined} %{?!_disable_lto:-flto} %{?_hardened_flags}

# Deprecated names.  For backwards compatibility only.
%ldflags %build_ldflags
%__global_cflags %{build_cflags}
%__global_cxxflags %{build_cxxflags}
%__global_fflags %{build_fflags}
%__global_fcflags %{build_fflags}
%__global_ldflags %{build_ldflags}

%set_build_flags \
  CC="${CC:-%__cc}" ; export CC ; \
  CXX="${CXX:-%__cxx}" ; export CXX ; \
  CFLAGS="${CFLAGS:-%build_cflags}" ; export CFLAGS ; \
  CXXFLAGS="${CXXFLAGS:-%build_cxxflags}" ; export CXXFLAGS ; \
  FFLAGS="${FFLAGS:-%build_fflags}" ; export FFLAGS ; \
  FCFLAGS="${FCFLAGS:-%build_fflags}" ; export FCFLAGS ; \
  %{?build_ldflags:LDFLAGS="${LDFLAGS:-%{build_ldflags}}"; export LDFLAGS ;} \
  LT_SYS_LIBRARY_PATH="${LT_SYS_LIBRARY_PATH:-%_libdir:}" ; export LT_SYS_LIBRARY_PATH ; \
%if %{cross_compiling} \
    PKG_CONFIG_PATH=/usr/%{_target_platform}/sys-root%{_libdir}/pkgconfig:/usr/%{_target_platform}/sys-root%{_datadir}/pkgconfig:%{_libdir}/pkgconfig:%{_datadir}/pkgconfig:$PKG_CONFIG_PATH; export PKG_CONFIG_PATH; \
%ifnarch noarch \
    CROSSCOMPILE="%{?!noconftarget:--target=%{_target_platform}} %{?!noconfhost:--host=%{_target_platform}} %{?!noconfbuild:--build=%{_build}}" ; \
%endif \
%else \
%ifnarch noarch \
    CROSSCOMPILE="%{?!noconftarget:--target=%{_target_platform}} %{?!noconfbuild:--build=%{_target_platform}}" ; \
%endif \
%endif \
  %{?cross:CROSSCOMPILE="%{?!noconftarget:--target=%{cross}} %{?!noconfhost:--host=%{_target_platform}} %{?!noconfbuild:--build=%{_build}}" ; } \
  export CROSSCOMPILE ; \
  %{nil}

%setup_compile_flags \
  echo "%%setup_compile_flags is deprecated, please update the spec to use %%set_build_flags instead" >&2; \
  %set_build_flags

%debugcflags %{?_enable_debug_packages:-g3 -gdwarf-4 -Wstrict-aliasing=2}

# common compilation flags

%_fortify_cflags -D_FORTIFY_SOURCE=2

# cf http://wiki.mandriva.com/en/Development/Packaging/Problems#format_not_a_string_literal_and_no_format_arguments
%Werror_cflags -Wformat -Werror=format-security

%_ssp_cflags -fstack-protector-all --param=ssp-buffer-size=4
%__common_cflags -Os -fomit-frame-pointer %{debugcflags} -pipe %{Werror_cflags} %{?_fortify_cflags}
%__common_cflags_with_ssp %{__common_cflags} %{?_ssp_cflags}

# Servers opt flags.
# No longer useful since the flags are active always now...
%serverbuild %{nil}

# Hardened Servers opt flags.
# Also set the env variables for backward compatibility (#32050).
%serverbuild_hardened %define _hardened_flags -pie -Wl,-z,now

# Include minimal debug information in build binaries.
# Requires _enable_debug_packages.
#
%_enable_debug_packages 1
%_include_minidebuginfo 1
%_include_gdb_index     1
%_debugsource_packages  1
%_debuginfo_subpackages 1
# Put build ID files into debug packages to save some space and FS cluttering
# on normal systems. The problem Fedora tried to solve by putting the build ID
# files into the main package (coinstallability for debuginfo packages on symbol server)
# doesn't apply to us because we cannot run a debug symbol server.
%_build_id_links alldebug

#==============================================================================
# ---- GPG/PGP/PGP5 signature macros.
#	Macro(s) to hold the arguments passed to GPG/PGP for package
#	signing and verification.
#

#	Verify digest/signature flags for various rpm modes:
#	0x30300 (_RPMVSF_NODIGESTS)    --nohdrchk      if set, don't check digest(s)
#	0xc0c00 (_RPMVSF_NOSIGNATURES) --nosignature   if set, don't check signature(s)
#	0xf0000 (_RPMVSF_NOPAYLOAD)    --nolegacy      if set, check header+payload (if possible)
#	0x00f00 (_RPMVSF_NOHEADER)     --nohdrchk      if set, don't check rpmdb headers
%_vsflags_query 0xc0c00

# enable nofsync when rebuilding rpmdb
%__dbi_rebuild nofsync

#	Open all indices before doing chroot(2).
#
%_openall_before_chroot	1

%_repackage_all_erasures 0

# activate filetriggers (cf http://wiki.mandriva.com/en/Rpm_filetriggers)
%_filetriggers_dir /var/lib/rpm/filetriggers

%_sys_macros_dir %{_sysconfdir}/rpm/macros.d
%build_sysmacrospath() %{_sys_macros_dir}/%{?1:%{1}}%{?!1:%{name}}.macros

# when %_with_git_repository is set, these macros modify the behaviour of "%prep" step:
%_after_setup %{?_with_git_repository:GIT_URL="%{?git_url}" GIT_REPOSITORY_CACHE=%{?git_repository_cache} /usr/lib/rpm/git-repository--after-tarball}
%_patch %{?_with_git_repository:PKG_NAME=%{name} /usr/lib/rpm/git-repository--apply-patch}%{?!_with_git_repository:%__patch --fuzz=%{_default_patch_fuzz} %{_default_patch_flags}}

# used by "git-repository--after-tarball":
%git_repository_cache %{_topdir}/%{name}.git

# Various programs used in rpm scripts
%_update_desktop_database_bin %{_bindir}/update-desktop-database
%_update_mime_database_bin %{_bindir}/update-mime-database
%_update_icon_cache_bin %{_bindir}/gtk-update-icon-cache
%_gconftool_bin %{_bindir}/gconftool-2
%_scrollkeeper_bin %{_bindir}/scrollkeeper-update

#==============================================================================
# ---- Required rpmrc macros.
#	Macros that used to be initialized as a side effect of rpmrc parsing.
#	These are the default values that can be overridden by other
#	(e.g. per-platform, per-system, per-packager, per-package) macros.
#

%_arch_tag_suffix %([ "%{?_lib}" = "lib64" ] && echo "()(64bit)")
%arch_tagged() %{1}%{_arch_tag_suffix} %{?2:%{2} %{3}%{?!3:%{error:undefined 3rd argument in arch_tagged}}}

# Games macros
%_gamesdir	games
%_gamesbindir   %{_prefix}/%{_gamesdir}
%_gamesdatadir  %{_datadir}/%{_gamesdir}

# Icon directories
%_iconsdir %{_datadir}/icons
%_miconsdir %{_datadir}/icons/mini
%_liconsdir %{_datadir}/icons/large

%_xfontdir %_datadir/fonts

%_webconfdir %{_sysconfdir}/httpd/conf
%_webappconfdir %_webconfdir/webapps.d

# For Appdata metainfo
%_metainfodir %{_datadir}/metainfo

#==============================================================================
# ---- Build configuration macros.
#
# Use internal dependency generator rather than external helpers?
%_use_internal_dependency_generator	1

# Update Window Managers session.
%_fndsession_bin %{_sbindir}/fndSession
%make_session %{nil}
%make_dm_session if [ -x %{_fndsession_bin} ]; then %{_fndsession_bin} || true ; fi \
%{nil}

# Rebuild icon cache
%update_icon_cache() if [ -x %{_update_icon_cache_bin} ]; then \
%{_update_icon_cache_bin} --force --quiet %{_iconsdir}/%{1} || true; fi \
%{nil}

%clean_icon_cache() if [ -x %{_update_icon_cache_bin} -a -r %{_iconsdir}/%{1}/index.theme ]; then \
%{_update_icon_cache_bin} --force --quiet %{_iconsdir}/%{1} || true ; fi \
%{nil}

# GConf schemas:
# installation is handled by filetriggers
%post_install_gconf_schemas() %{nil}
# but uninstall still need to be done in %preun:
%preun_uninstall_gconf_schemas() if [ "$1" = "0" -a -x %{_gconftool_bin} ]; then \
SCHEMAS="" \
for SCHEMA in %{*} ; do \
  SCHEMAS="$SCHEMAS %{_sysconfdir}/gconf/schemas/$SCHEMA.schemas" \
done \
GCONF_CONFIG_SOURCE=`%{_gconftool_bin} --get-default-source` %{_gconftool_bin} --makefile-uninstall-rule  $SCHEMAS > /dev/null || true ; fi \
%{nil}

# Mandriva Linux version
# - "9.1"    =>  910
# - "10.2.2" => 1022
# (user may copy the following line in specfile)
%mdkversion		%(cat /etc/os-release|grep VERSION_ID= |sed -e 's,.*=,,;s,\",,g;s,\\.,0,g')


#==============================================================================
# ---- Build policy macros.
#
#---------------------------------------------------------------------
#   Expanded at end of %install scriptlet.
#

%__os_install_post    \
    %{?__spec_helper_post}%{?!__spec_helper_post:/usr/share/spec-helper/spec-helper} \
    [ %{?_python_bytecompile_build} -eq 1 ] && %{__brp_python_bytecompile} \
    %{!?__brp_python_hardlink:/usr/lib/rpm/brp-python-hardlink} \
%{nil}

%__os_stringbuf_post    \
    %{?__spec_helper_stringbuf_post} \
%{nil}


#--------------------------------------------------------------------------------
# Macro from conectiva

# Shorthand for %{defined with_...}
# macros provided by rpm 4.4, but buggy :(
%_with() %{expand: %%{?_with_%1:1} %%{!?_with_%1: %%{?_without_%1:0} %%{!?_without_%1: %%{?with_%1:%%{with_%1}} %%{!?with_%1: %%{?without_%1:!%%{without_%1}} %%{!?without_%1: %%{?2:%%2} %%{!?2:1} } } } } }
%_without() !%{expand: %%{with %1 %{?2:%2}}}

%_package_i18n(g:f:) \
%%package -n %{?1:%{1}}%{?!1:%{name}}-i18n\
Summary: Internationalization and locale data for %{?1:%{1}}%{?!1:%{name}}\
Group: %{?-g:%{-g*}}%{?!-g:%{group}}\
\
%%description -n %{?1:%{1}}%{?!1:%{name}}-i18n\
Internationalization and locale data for %{?1:%{1}}%{?!1:%{name}}\
\
%%files -n %{?1:%{1}}%{?!1:%{name}}-i18n %{?-f:%{-f}}%{?!-f:-f %{?1:%{1}}%{?!1:%{name}}.lang}\
%{nil}

#------------------------------------------------------------------------------
# get dependencies from library that foo.so points to, very convenient if
# one wants to ie. generate requires/suggests against libraries that are
# dlopen()'ed.
# First argument specifies library name without .so extension and lib prefix,
# while the optional second argument can be used to specify different library
# path than %%{_libdir}
%dlopen_req() %{shrink:\
%([ -e %{?!2:%{_libdir}}%{?2}/lib%{1}.so ] && \
rpm -qf --fileprovide $(readlink -f %{?!2:%{_libdir}}%{?2}/lib%{1}.so) 2>/dev/null | \
grep $(readlink -f %{?!2:%{_libdir}}%{?2}/lib%{1}.so) | cut -f2 || echo %{name})}

# get soname of libfoo.so
# usage:
# %%{lib_soname intl} expands to libintl.so.8
#
# default library search path is %%{_libdir}, which you can override with an
# optional second argument:
# %%{lib_soname intl %%{uclibc_root}%%{_libdir}}

%lib_soname() %(\
[ -e %{?!2:%{_libdir}}%{?2}/lib%{1}.so ] && \
objdump -p %{?!2:%{_libdir}}%{?2}/lib%{1}.so | grep -e SONAME | sed -e 's#.*\\\(lib.*\\\)\$#\\\1#g'\
)

#------------------------------------------------------------------------------
# arch macros
# arch macro for all EFI-compatible architectures
%efi %{ix86} %{x86_64} %{aarch64} %{riscv} ia64

# macro that contains all arm archs, even 64 bit ones
%armx %arm aarch64

# Get the base architecture (e.g. i386 for any x86, arm for any arm)
#
%_base_arch() %(rpm --target='%1' -E '%%{_arch}' | tr -d '\n')

# 1 if cross-compiling, 0 if doing a regular build
# Note: In order to avoid ie. building for i586 on x86_64 being treated as if
# cross-compiling, a check to see if base arch is listed with 'setarch --list'
# is performed.
#

%cross_compiling %(rm -f /tmp/rpm_cc_test 2>/dev/null; echo 'int main() { return 0; }' | %{__cc} %{optflags} -x c - -o /tmp/rpm_cc_test &>/dev/null; if /tmp/rpm_cc_test 2>/dev/null; then echo -n 0; else echo -n 1; fi; rm -f /tmp/rpm_cc_test 2>/dev/null)

#------------------------------------------------------------------------------
# LEGACY RPM5 macro compatibility - packages using these need to be fixed!
%_usrlibrpm %{_rpmconfigdir}
%_etcrpm %{_sysconfdir}/rpm
%_sys_macros_dir %{_rpmmacrodir}
