%_vendor           TencentOS

# ---- VPATH default settings fro cmake
%_vpath_builddir %_target_platform
%_vpath_srcdir .

%_gnu -gnu

%_target_platform  %{_target_cpu}-%{_vendor}-%{_target_os}%{?_gnu}

%_enable_debug_packages 1
%_include_minidebuginfo 1
%_include_gdb_index     1
%_debugsource_packages  1
%_debuginfo_subpackages 1


%__brp_ldconfig /usr/lib/rpm/%{_vendor}/brp-ldconfig
%__brp_compress /usr/lib/rpm/brp-compress
%__brp_strip /usr/lib/rpm/brp-strip %{__strip}
%__brp_strip_lto /usr/lib/rpm/%{_vendor}/brp-strip-lto %{__strip}
%__brp_strip_comment_note /usr/lib/rpm/brp-strip-comment-note %{__strip} %{__objdump}
%__brp_strip_static_archive /usr/lib/rpm/brp-strip-static-archive %{__strip}
%__brp_check_rpaths /usr/lib/rpm/check-rpaths
# __brp_mangle_shebangs_exclude - shebangs to exclude
# __brp_mangle_shebangs_exclude_file - file from which to get shebangs to exclude
# __brp_mangle_shebangs_exclude_from - files to ignore
# __brp_mangle_shebangs_exclude_from_file - file from which to get files to ignore
%__brp_mangle_shebangs /usr/lib/rpm/%{_vendor}/brp-mangle-shebangs %{?__brp_mangle_shebangs_exclude:--shebangs "%{?__brp_mangle_shebangs_exclude}"} %{?__brp_mangle_shebangs_exclude_file:--shebangs-from "%{__brp_mangle_shebangs_exclude_file}"} %{?__brp_mangle_shebangs_exclude_from:--files "%{?__brp_mangle_shebangs_exclude_from}"} %{?__brp_mangle_shebangs_exclude_from_file:--files-from "%{__brp_mangle_shebangs_exclude_from_file}"}

%__brp_llvm_compile_lto_elf /usr/lib/rpm/%{_vendor}/brp-llvm-compile-lto-elf %{build_cflags} %{build_ldflags}

%__os_install_post    \
    %[ "%{toolchain}" == "clang" ? "%{?__brp_llvm_compile_lto_elf}" : "%{nil}" ] \
    %{?__brp_ldconfig} \
    %{?__brp_compress} \
    %{!?__debug_package:\
    %{?__brp_strip} \
    %{?__brp_strip_comment_note} \
    } \
    %{?__brp_strip_lto} \
    %{?__brp_strip_static_archive} \
    %{?__brp_check_rpaths} \
    %{?__brp_mangle_shebangs} \
    %{__os_install_post_python} \
%{nil}


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

%__cc_clang clang
%__cxx_clang clang++
%__cpp_clang clang-cpp

%toolchain gcc

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

%_pkgdocdir %{_docdir}/%{name}
%_docdir_fmt            %%{NAME}
%_localstatedir       /var
%_fmoddir           %{_libdir}/gfortran/modules

%_hardening_gcc_cflags   -specs=/usr/lib/rpm/%{_vendor}/TencentOS-hardened-cc1
%_hardening_clang_cflags -fPIE
%_hardening_cflags   %{expand:%%{_hardening_%{toolchain}_cflags}} -fstack-protector-strong
%_hardening_ldflags -Wl,-z,now %[ "%{toolchain}" == "gcc" ? "-specs=/usr/lib/rpm/%{_vendor}/TencentOS-hardened-ld" : "" ]

%_hardened_build 1
%_hardened_cflags %{?_hardened_build:%{_hardening_cflags}}
%_hardened_ldflags  %{?_hardened_build:%{_hardening_ldflags}}


%_gcc_lto_cflags -flto=auto -ffat-lto-objects
%_clang_lto_cflags -flto
%_lto_cflags %{expand:%%{_%{toolchain}_lto_cflags}}

%_legacy_options    %{?_legacy_common_support: -fcommon}

%__global_compiler_flags -O2 %{?_lto_cflags} -fexceptions -g -grecord-gcc-switches -pipe -Wall -Werror=format-security -Wp,-D_FORTIFY_SOURCE=2 -Wp,-D_GLIBCXX_ASSERTIONS %{_hardened_cflags} %{_legacy_options}

%__cflags_arch_x86_64 -march=x86-64-v2
%__cflags_arch_ppc64le -mcpu=power9 -mtune=power9

%_configure_gnuconfig_hack     1
%_configure_libtool_hardening_hack     1

%_ld_symbols_flags %{?_ld_strict_symbol_defs:-Wl,-z,defs}
%_ld_as_needed 1
%_ld_as_needed_flags %{?_ld_as_needed:-Wl,--as-needed}


# C compiler flags. also called CFLAGS in makefiles.
%build_cflags %{optflags}

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

# Fortran compiler flags.  called FFLAGS and FCFLAGS in makefiles.
%build_fflags %{optflags} -I%{_fmoddir}

%_build_id_flags    -Wl,--build-id=sha1

%build_ldflags -Wl,-z,relro %{_ld_as_needed_flags} %{_ld_symbols_flags} %{_hardened_ldflags} %[ "%{toolchain}" == "clang" ? "%{?_lto_cflags}" : "" ] %{_build_id_flags}

%__global_ldflags %{build_ldflags}
%__global_cflags %{build_cflags}

# Arches Valgrind works on
%valgrind_arches x86_64 aarch64 ppc64le loongarch64

# Do not use this directly, internal use only.
# Helper macro for cflags, cxxflags, fflags, ldflags extension, see below where it is invoked.
# 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("%{build_" .. name .. "}")
local specs_pattern = "%s+-specs=[^%s]+"
local lto_flags_pattern = rpm.expand("%{?_lto_cflags}"):gsub("[%-%.]", "%%%1")
local package_note_flags_pattern = "%-Wl,%S*package_note%S*"
local result = value:gsub(specs_pattern, " "):gsub(lto_flags_pattern, ""):gsub(package_note_flags_pattern, "")
print(result)
}

# Variants of CFLAGS, CXXFLAGS, FFLAGS, LDFLAGS for use within
# extension builders.
%extension_cflags %{__extension_strip_flags cflags}
%extension_cxxflags %{__extension_strip_flags cxxflags}
%extension_fflags %{__extension_strip_flags fflags}
%extension_ldflags %{__extension_strip_flags ldflags}

# Expands to shell code to set the compiler/linker environment
# variables CFLAGS, CXXFLAGS, FFLAGS, FCFLAGS, LDFLAGS if they have
# not been set already.  RPM_OPT_FLAGS and RPM_LD_FLAGS have already
# been set implicitly at the start of the %%build section.
# LT_SYS_LIBRARY_PATH is used by libtool script.
%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 ; \
  CC="${CC:-%{__cc}}" ; export CC ; \
  CXX="${CXX:-%{__cxx}}" ; export CXX

# Automatically use set_build_flags macro for build, check, and
# install phases.
# Use "%undefine _auto_set_build_flags" to disable"
%_auto_set_build_flags 1
%__spec_build_pre %{___build_pre} \
  %{?_auto_set_build_flags:%{set_build_flags}}

%__spec_check_pre %{___build_pre} \
  %{?_auto_set_build_flags:%{set_build_flags}}


#==============================================================================
# ---- Generic auto req/prov filtering macros

# prevent anything matching from being scanned for provides
%filter_provides_in(P) %{expand: \
%global __filter_prov_cmd %{?__filter_prov_cmd} %{__grep} -v %{-P} '%*' | \
}

# prevent anything matching from being scanned for requires
%filter_requires_in(P) %{expand: \
%global __filter_req_cmd %{?__filter_req_cmd} %{__grep} -v %{-P} '%*' | \
}

# filter anything matching out of the provides stream
%filter_from_provides() %{expand: \
%global __filter_from_prov %{?__filter_from_prov} | %{__sed} -e '%*' \
}

# filter anything matching out of the requires stream
%filter_from_requires() %{expand: \
%global __filter_from_req %{?__filter_from_req} | %{__sed} -e '%*' \
}

# actually set up the filtering bits
%filter_setup %{expand: \
%global _use_internal_dependency_generator 0 \
%global __deploop() while read FILE; do echo "${FILE}" | /usr/lib/rpm/rpmdeps -%{1}; done | /bin/sort -u \
%global __find_provides /bin/sh -c "%{?__filter_prov_cmd} %{__deploop P} %{?__filter_from_prov}" \
%global __find_requires /bin/sh -c "%{?__filter_req_cmd}  %{__deploop R} %{?__filter_from_req}" \
}


%rpmmacrodir /usr/lib/rpm/macros.d
%_metainfodir %{_datadir}/metainfo


%lua_version %{lua: print(string.sub(_VERSION, 5))}
%lua_libdir %{_libdir}/lua/%{lua_version}
%lua_pkgdir %{_datadir}/lua/%{lua_version}

%lua_requires \
Requires: lua(abi) = %{lua_version} \
%{nil}

%_fix_broken_configure_for_lto \
  for file in $(find . -type f -name configure -print); do \
    %{__sed} -r --in-place=.backup 's/^char \\(\\*f\\) \\(\\) = /__attribute__ ((used)) char (*f) () = /g' $file; \
    diff -u $file.backup $file && mv $file.backup $file \
    %{__sed} -r --in-place=.backup 's/^char \\(\\*f\\) \\(\\);/__attribute__ ((used)) char (*f) ();/g' $file; \
    diff -u $file.backup $file && mv $file.backup $file \
    %{__sed} -r --in-place=.backup 's/^char \\$2 \\(\\);/__attribute__ ((used)) char \\$2 ();/g' $file; \
    diff -u $file.backup $file && mv $file.backup $file \
    %{__sed} --in-place=.backup '1{$!N;$!N};$!N;s/int x = 1;\\nint y = 0;\\nint z;\\nint nan;/volatile int x = 1; volatile int y = 0; volatile int z, nan;/;P;D' $file; \
    diff -u $file.backup $file && mv $file.backup $file \
    %{__sed} --in-place=.backup 's#^lt_cv_sys_global_symbol_to_cdecl=.*#lt_cv_sys_global_symbol_to_cdecl="sed -n -e '"'"'s/^T .* \\\\(.*\\\\)$/extern int \\\\1();/p'"'"' -e '"'"'s/^$symcode* .* \\\\(.*\\\\)$/extern char \\\\1;/p'"'"'"#' $file; \
    diff -u $file.backup $file && mv $file.backup $file \
  done

%configure \
  [ "%{_lto_cflags}"x != x ] && %{_fix_broken_configure_for_lto}; \
  [ "%_configure_gnuconfig_hack" = 1 ] && for i in $(find $(dirname %{_configure}) -name config.guess -o -name config.sub) ; do \
      [ -f /usr/lib/rpm/%{_vendor}/$(basename $i) ] && %{__rm} -f $i && %{__cp} -fv /usr/lib/rpm/%{_vendor}/$(basename $i) $i ; \
  done ; \
  [ "%_configure_libtool_hardening_hack" = 1 ] && [ x != "x%{_hardened_ldflags}" ] && \
      for i in $(find . -name ltmain.sh) ; do \
        %{__sed} -i.backup -e 's~compiler_flags=$~compiler_flags="%{_hardened_ldflags}"~' $i \
      done ; \
  %{_configure} --build=%{_build} --host=%{_host} \\\
    --program-prefix=%{?_program_prefix} \\\
    --disable-dependency-tracking \\\
    %{?_configure_disable_silent_rules:--disable-silent-rules} \\\
    --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}

%_smp_mflags %([ -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; \\\
        if [ "$RPM_BUILD_NCPUS" -gt 1 ]; then echo "-j$RPM_BUILD_NCPUS"; fi)

%__spec_install_pre %{___build_pre}\
    [ "$RPM_BUILD_ROOT" != "/" ] && rm -rf "${RPM_BUILD_ROOT}"\
    mkdir -p `dirname "$RPM_BUILD_ROOT"`\
    mkdir "$RPM_BUILD_ROOT"\
%{nil}

%__arch_install_post   /usr/lib/rpm/check-buildroot

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

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

%_missing_build_ids_terminate_build    1

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

# Use XZ compression for binary payloads
%_binary_payload w2.xzdio


%gpgverify(k:s:d:) %{lua:
local script = rpm.expand("%{_rpmconfigdir}/%{_vendor}/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
}


%_rpmconfigdir          %{getconfdir}
%_rpmmacrodir           %{_rpmconfigdir}/macros.d

%__perl %{_bindir}/perl

%golang_arches   x86_64 aarch64 loongarch64 ppc64le

%rust_arches x86_64 aarch64 loongarch64 ppc64le riscv64

%listfiles(i:x:) %{expand:
%if %{lua: print(string.len(rpm.expand("%{?-i*}%{?listfiles_include}%*")))}
  listfiles_include=$(realpath -e --relative-base=. %{?-i*} %{?listfiles_include} %* | sort -u)
  %if  %{lua: print(string.len(rpm.expand("%{?-x*}%{?listfiles_exclude}")))}
    while IFS= read -r finc ; do
      realpath -qe --relative-base=. %{?-x*} %{?listfiles_exclude} \\
        | sort -u | grep -q "${finc}" || echo "${finc}"
    done <<< "${listfiles_include}"
  %else
    echo "${listfiles_include}"
  %endif
%endif
}

%bash_completions_dir   %{_datadir}/bash-completion/completions
%zsh_completions_dir    %{_datadir}/zsh/site-functions
%fish_completions_dir   %{_datadir}/fish/vendor_completions.d

%mono_arches x86_64

%writevars(f:) %{lua:
local TencentOS = require "TencentOS.common"
local macrofile = rpm.expand("%{-f*}")
local   rpmvars = {}
for i = 1, rpm.expand("%#") do
  table.insert(rpmvars, rpm.expand("%" .. i))
end
TencentOS.writevars(macrofile,rpmvars)
}
