# directories
%_infodir               %{_prefix}/share/info
%_mandir                %{_prefix}/share/man
%_sysconfdir            /etc
%_localstatedir         /var
%_defaultdocdir         %{_datadir}/doc
%_pkgdocdir             %{_docdir}/%{name}
%_docdir_fmt            %%{NAME}
%_firmwaredir           %{_usr}/lib/firmware

# package build macros
%make_install           make install DESTDIR="%{?buildroot}"

# this script calls all scripts in /usr/lib/rpm/brp-openruyi.d
%__os_install_post  \
	/usr/lib/rpm/brp-compress \
    %{!?__debug_package:\
    %{?__brp_strip} \
    } \
    %{?__brp_remove_la_files} \
	/usr/lib/rpm/brp-openruyi \
%{nil}

%run_ldconfig /sbin/ldconfig

# find-supplements.ksyms parses this macro directly out of the spec file:
%supplements_kernel_module() \
    %{expand:%(if ! rpm -q kernel-syms > /dev/null; then echo "%fail Please add the kernel-syms package to BuildRequires"; fi)}

%do_profiling 1
%cflags_profile_generate -fprofile-update=atomic -fprofile-generate
%cflags_profile_feedback -fprofile-use

# package version comparison macros

# compare two versions, returns -1, 0, 1, ~~~
%rpm_vercmp() %{lua:print(rpm.expand('%1') == '~~~' and '~~~' or rpm.vercmp(rpm.expand('%1'), rpm.expand('%2')))}

# expand to the installed version of a package/provides
%pkg_version() %(LC_ALL=C rpm -q --whatprovides --qf "%%{version}" "%1" | sed -e "s/^no package provides .*/~~~/")

# compare the installed version to a value. returns -1, 0, 1, ~~~
%pkg_version_cmp() %{expand:%{expand:%%%%{rpm_vercmp %%{pkg_version %1} %%2}}}

# convenience/readability
%pkg_vcmp() (%{expand:%%{pkg_version_cmp %1 %3}} %2 0)

# to be removed when added to the javapackages-tools package
%_jvmjardir     %{_libdir}/jvm-exports

# dropped from rpm package
%requires_eq() %{expand:%(t=$(echo '%*' | LC_ALL=C xargs -r rpm -q --qf 'Requires: %%{name} = %%{epoch}:%%{version}\\n' | sed -e 's/ (none):/ /' -e 's/ 0:/ /' | grep -v "is not"); test -n "$t" || echo "%%{error: %%%%{requires_eq %*} does not resolve}"; echo $t)}
%requires_ge() %{expand:%(t=$(echo '%*' | LC_ALL=C xargs -r rpm -q --qf 'Requires: %%{name} >= %%{epoch}:%%{version}\\n' | sed -e 's/ (none):/ /' -e 's/ 0:/ /' | grep -v "is not"); test -n "$t" || echo "%%{error: %%%%{requires_eq %*} does not resolve}"; echo $t)}
%__perl			/usr/bin/perl

# we use the macro as one can easily override it and thus disable LTO for a particular package
%_lto_cflags		-flto=auto -ffat-lto-objects

# Security Hardening
%openruyi_security_cflags   -fstack-protector-strong -D_FORTIFY_SOURCE=3 -fstack-clash-protection -Wformat -Werror=format-security
%openruyi_security_ldflags  -Wl,-z,relro,-z,now,-z,noexecstack
%openruyi_security_cxxflags -D_GLIBCXX_ASSERTIONS

# General Compiler Flags
%openruyi_general_flags     -O2 -g -fno-omit-frame-pointer -pipe %{_lto_cflags} -funwind-tables -fasynchronous-unwind-tables

# Architecture Specific
%openruyi_riscv_arch   -march=rva23u64
%openruyi_riscv_tune   -mtune=generic-ooo

%openruyi_optflags_noarch  %{nil}
%openruyi_optflags_riscv64 %{openruyi_general_flags} %{openruyi_security_cflags} %{openruyi_riscv_arch} %{openruyi_riscv_tune}
%openruyi_optflags_x86_64  %{openruyi_general_flags} %{openruyi_security_cflags}

%__global_cflags()   %{expand:%{?_target_cpu:%%{openruyi_optflags_%{_target_cpu}}}}
%__global_cxxflags() %{__global_cflags} %{openruyi_security_cxxflags}
%__global_ldflags()  %{openruyi_security_ldflags}

%build_cflags      %{__global_cflags}
%build_cxxflags    %{__global_cxxflags}
%build_ldflags     %{__global_ldflags}

# avoid overwriting files that didn't actually change on disk
%_minimize_writes	1

# we expects bash for building
%_buildshell           /usr/bin/bash

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

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

# Use zstd for binary payloads
%_binary_payload w19T0.zstdio

# Expands to shell code to set the compiler/linker environment
# variables CFLAGS, CXXFLAGS, FFLAGS, FCFLAGS, LDFLAGS if they have
# not been set already.
%set_build_flags \
  CONFIG_SHELL="${CONFIG_SHELL:-/usr/bin/bash}" ; export CONFIG_SHELL ; \
  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 ; \
  CC=%{__cc}; export CC ; \
  CXX=%{__cxx}; export CXX 
