#
#   System settings
#
%_topdir            /usr/src/azl
#
#   Some standard locations
#
%_sysconfdir        /etc
%_lib               lib
%_lib64             lib64
%_prefix            /usr
%_exec_prefix       %{_prefix}
%_bindir            %{_exec_prefix}/bin
%_sbindir           %{_exec_prefix}/sbin
%_libdir            /usr/%{_lib}
%_lib64dir          /usr/%{_lib64}
%_libexecdir        %{_exec_prefix}/libexec
%_datadir           %{_prefix}/share
%_docdir            %{_datadir}/doc
%_includedir        %{_prefix}/include
%_infodir           %{_datadir}/info
%_mandir            %{_datadir}/man
%_oldincludedir     /usr/include
%_sharedstatedir    /var/lib
%_var               /var
%_localstatedir     /var

# https://fedoraproject.org/wiki/Changes/UnversionedDocdirs
%_pkgdocdir         %{_docdir}/%{name}
%_docdir_fmt        %%{NAME}

%__tar              /bin/tar --no-same-owner

%_fmoddir           %{_libdir}/gfortran/modules

# Generate metadata linker script before prep section of each SPEC
%__spec_prep_template   #!%{__spec_prep_shell}\
%{__spec_prep_pre}\
/usr/lib/rpm/azl/gen-ld-script.sh %{name} %{?epoch:%{epoch}:}%{version}-%{release} %{_topdir} %{distro_release_version}\
%{nil}

# This section overrides the default behavior of the build macro to export
# all of the flags handled by set_build_flags. Once the configure macros
# are consistently applied this override should be removed.
#
# USE_CCACHE: when 'ccache_enabled' is defined, enable building with ccache
#
%__spec_build_template	#!%{__spec_build_shell}\
%{__spec_build_pre}\
%{set_build_flags}\
%{?ccache_enabled:PATH="/usr/lib/ccache:$PATH" ; export PATH ;}\
%{?ccache_enabled:CCACHE_DIR="/ccache-dir" ; export CCACHE_DIR ;}\
%{?ccache_enabled:CCACHE_COMPILERCHECK=content ; export CCACHE_COMPILERCHECK ;}\
%{?ccache_enabled:ccache --zero-stats} \
%{nil}

# use zstd compression for binary package payloads
%_binary_payload w7T0.zstdio

#
## Automatically compile python files
%py_auto_byte_compile 1

#
## Should python bytecompilation errors terminate a build?
%_python_bytecompile_errors_terminate_build 1
## Should python bytecompilation compile outside python specific directories?
%_python_bytecompile_extra 0

%__brp_python_bytecompile /usr/lib/rpm/azl/brp-python-bytecompile "%{__python3}" "%{?_python_bytecompile_errors_terminate_build}" "%{?_python_bytecompile_extra}"

%_enable_debug_packages 1
%_include_minidebuginfo 1
%_use_internal_dependency_generator 1
%_build_id_links alldebug

# 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_comment_note %{_rpmconfigdir}/brp-strip-comment-note %{__strip} %{__objdump}
%__brp_strip_static_archive %{_rpmconfigdir}/brp-strip-static-archive %{__strip}
%__brp_elfperms %{_rpmconfigdir}/brp-elfperms
%__brp_remove_la_files %{_rpmconfigdir}/brp-remove-la-files

%__os_install_post_leave_signatures    \
    %{?__brp_compress} \
    %{?__brp_strip_comment_note} \
    %{?__brp_strip_static_archive} \
    %{?py_auto_byte_compile:%{?__brp_python_bytecompile}} \
    find %{buildroot} -name "*.pc" | xargs -I{} sed -i -e 's@-Wl,-dT,%{_topdir}/BUILD/module_info.ld@ @' {} \
    %{nil}

%__os_install_post    \
    %{__os_install_post_leave_signatures} \
    %{?__brp_strip} \
    %{?ccache_enabled:CCACHE_DIR="/ccache-dir" ; export CCACHE_DIR ;} \
    %{?ccache_enabled:ccache --show-stats} \
    %{nil}

%install %{?_enable_debug_packages:%{?buildsubdir:%{debug_package}}}\
%%install\
%{nil}

# Binary macros
%__make %{_bindir}/make

# Enable linking with module_info.ld in Azure Linux build environment by defining: distro_module_ldflags  -Wl,-dT,%{_topdir}/BUILD/module_info.ld

# Compilation and linking flag macros
%build_cflags %{optflags}
%build_cxxflags %{optflags}
%build_fflags %{optflags} -I%{_fmoddir}
%build_ldflags -Wl,-z,relro %{_ld_as_needed_flags} %{_ld_symbols_flags} %{_hardened_ldflags} %{?distro_module_ldflags :%{distro_module_ldflags }}

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

%set_build_flags \
  CFLAGS="${CFLAGS:-%{build_cflags}}" ; export CFLAGS ; \
  CXXFLAGS="${CXXFLAGS:-%{build_cxxflags}}" ; export CXXFLAGS ; \
  FFLAGS="${FFLAGS:-%{build_fflags}}" ; export FFLAGS ; \
  FCFLAGS="${FCFLAGS:-%{build_fflags}}" ; export FCFLAGS ; \
  LDFLAGS="${LDFLAGS:-%{build_ldflags}}" ; export LDFLAGS ; \
  LT_SYS_LIBRARY_PATH="${LT_SYS_LIBRARY_PATH:-%_libdir:}" ; export LT_SYS_LIBRARY_PATH

# Expand a variable and strip the flags not suitable to extension builders.
%__extension_strip_flags() %{lua:
local name = rpm.expand("%{1}")
local value = " " .. rpm.expand("%{" .. name .. "}")
local specs_pattern = "%s+-specs=[^%s]+"
local lto_flags_pattern = rpm.expand("%{?_lto_cflags}"):gsub("[%-%.]", "%%%1")
local module_info_pattern = "%s*%-Wl,%-dT,[^%s]+module_info%.ld"
local leading_spaces_pattern = "^%s+"
local result = value:gsub(specs_pattern, " "):gsub(lto_flags_pattern, ""):gsub(module_info_pattern, ""):gsub(leading_spaces_pattern, "")
print(result)
}

# Variants of CFLAGS, CXXFLAGS, FFLAGS, LDFLAGS for use within
# extension builders.
# https://fedoraproject.org/wiki/Changes/Python_Extension_Flags
%extension_cflags %{__extension_strip_flags build_cflags}
%extension_cxxflags %{__extension_strip_flags build_cxxflags}
%extension_fflags %{__extension_strip_flags build_fflags}
%extension_ldflags %{__extension_strip_flags build_ldflags}

%configure \
  %{set_build_flags}; \
  %{_configure} --host=%{_host} --build=%{_build} \\\
        --program-prefix=%{?_program_prefix} \\\
        --disable-dependency-tracking \\\
        --prefix=%{_prefix} \\\
        --exec-prefix=%{_exec_prefix} \\\
        --bindir=%{_bindir} \\\
        --sbindir=%{_sbindir} \\\
        --sysconfdir=%{_sysconfdir} \\\
        --datadir=%{_datadir} \\\
        --includedir=%{_includedir} \\\
        --libdir=%{_libdir} \\\
        --libexecdir=%{_libexecdir} \\\
        --localstatedir=%{_localstatedir} \\\
        --sharedstatedir=%{_sharedstatedir} \\\
        --mandir=%{_mandir} \\\
        --infodir=%{_infodir}

# Use SHA256 for FILEDIGESTS
%_source_filedigest_algorithm 8
%_binary_filedigest_algorithm 8

%_hardening_cflags      -specs=/usr/lib/rpm/azl/default-hardened-cc1
%_hardening_ldflags     -Wl,-z,now -specs=/usr/lib/rpm/azl/default-hardened-ld

# Harden packages by default
# https://fedorahosted.org/fesco/ticket/1384 (accepted on 2014-02-11)
# Use "%undefine _hardened_build" to disable.
%_hardened_build        1
%_hardened_cflags       %{?_hardened_build:%{_hardening_cflags}}
%_hardened_ldflags      %{?_hardened_build:%{_hardening_ldflags}}

# Add extra information to binary objects created by gcc
# https://pagure.io/fesco/issue/1780 (accepted on 2017-10-30)
# Use "%undefine _annotated_build" to disable.
#%_annotated_build       1
%_annobin_cflags        -specs=/usr/lib/rpm/azl/default-annobin-cc1
%_annotated_cflags      %{?_annotated_build:%{_annobin_cflags}}

# 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}

# https://fedoraproject.org/wiki/Changes/RemoveExcessiveLinking
# use "%undefine _ld_as_needed" to disable.
%_ld_as_needed          1
%_ld_as_needed_flags    %{?_ld_as_needed:-Wl,--as-needed}

# If enabled, include frame pointer information
# Use "%define _include_frame_pointers 1" to enable
%_frame_pointers_cflags %{?_include_frame_pointers:-fno-omit-frame-pointer}

%_legacy_common_support 1
%__global_compiler_flags        -O2 -g -pipe -Wall -Werror=format-security -Wp,-D_FORTIFY_SOURCE=2 -Wp,-D_GLIBCXX_ASSERTIONS -fexceptions -fstack-protector-strong -grecord-gcc-switches %{_hardened_cflags} %{_annotated_cflags} %{_frame_pointers_cflags} %{?_legacy_common_support: -fcommon}

# https://src.fedoraproject.org/rpms/redhat-rpm-config/blob/master/f/macros.ldconfig
#%ldconfig /sbin/ldconfig
%ldconfig_post(n:) %{?ldconfig:%post -p %ldconfig %{?*} %{-n:-n %{-n*}}\
%end}
%ldconfig_postun(n:) %{?ldconfig:%postun -p %ldconfig %{?*} %{-n:-n %{-n*}}\
%end}
%ldconfig_scriptlets(n:) %{?ldconfig:\
%ldconfig_post %{?*} %{-n:-n %{-n*}}\
%ldconfig_postun %{?*} %{-n:-n %{-n*}}\
}

# gpgverify verifies signed sources. There is documentation in the script.
%gpgverify(k:s:d:) %{lua:
local script = rpm.expand("%{_libdir}/rpm/azl/gpgverify ")
local keyring = rpm.expand("%{-k*}")
local signature = rpm.expand("%{-s*}")
local data = rpm.expand("%{-d*}")
print(script)
if keyring ~= "" then
  print(rpm.expand("--keyring='%{SOURCE" .. keyring ..  "}' "))
end
if signature ~= "" then
  print(rpm.expand("--signature='%{SOURCE" .. signature ..  "}' "))
end
if data ~= "" then
  print(rpm.expand("--data='%{SOURCE" .. data ..  "}' "))
end
}

# ---- VPATH default settings

# directory where CMakeLists.txt/meson.build/etc. are placed
%_vpath_srcdir .

# directory (doesn't need to exist) where all generated build files will be placed
%_vpath_builddir %_target_platform

# Maximum number of CPU's to use when building, 0 for unlimited.
%_smp_ncpus_max 0

%_smp_build_ncpus %([ -z "$RPM_BUILD_NCPUS" ] \\\
	&& RPM_BUILD_NCPUS="`/usr/bin/getconf _NPROCESSORS_ONLN`"; \\\
        ncpus_max=%{?_smp_ncpus_max}; \\\
        if [ -n "$ncpus_max" ] && [ "$ncpus_max" -gt 0 ] && [ "$RPM_BUILD_NCPUS" -gt "$ncpus_max" ]; then RPM_BUILD_NCPUS="$ncpus_max"; fi; \\\
        echo "$RPM_BUILD_NCPUS";)

%_smp_mflags "-j%{_smp_build_ncpus}"

# A directory for appdata metainfo.
%_metainfodir %{_datadir}/metainfo

# New version of RPM has stricter approach to encoding; turn the errors off temporarily.
%_invalid_encoding_terminates_build 0

# Azure Linux's sources storage URL.
# NOTE: only allowed to be used for source URLs when original, upstream source cannot be found.
%_distro_sources_url https://azurelinuxsrcstorage.blob.core.windows.net/sources/core
