## ======================================================================

%_rpmconfigdir		%{getconfdir}

## Vendor specific macros
## ======================================================================

%vendor_name		%{?_with_vendor_name}%{!?_with_vendor_name:OSSTech Corp.}
%vendor_nickname	%{?_with_vendor_nickname}%{!?_with_vendor_nickname:OSSTech}
%vendor_url		%{?_with_vendor_url}%{!?_with_vendor_url:https://www.osstech.co.jp/}
%vendor_id		%{?_with_vendor_id}%{!?_with_vendor_id:osstech}
%vendor_info		%{?_with_vendor_info}%{!?_with_vendor_info:OSSTech Corp. <https://www.osstech.co.jp/>}
%vendor_name_prefix	%{vendor_id}-

## ----------------------------------------------------------------------

%vendor_prefix		/opt/%{vendor_id}
%vendor_exec_prefix	%{vendor_prefix}
%vendor_var		%{vendor_prefix}/var
%vendor_bindir		%{vendor_exec_prefix}/bin
%vendor_sbindir		%{vendor_exec_prefix}/sbin
%vendor_libdir		%{vendor_exec_prefix}/%{_lib}
%vendor_libexecdir	%{vendor_exec_prefix}/libexec
%vendor_includedir	%{vendor_prefix}/include
%vendor_sysconfdir	%{vendor_prefix}/etc
%vendor_sharedstatedir	%{vendor_prefix}/var/lib
%vendor_localstatedir	%{vendor_prefix}/var
%vendor_rundir		%{vendor_localstatedir}/run
%vendor_vardir		%{vendor_sharedstatedir}
%vendor_datadir		%{vendor_prefix}/share
%vendor_infodir		%{vendor_prefix}/share/info
%vendor_mandir		%{vendor_prefix}/share/man
%vendor_docdir		%{vendor_prefix}/share/doc
%vendor_svdir		%{vendor_localstatedir}/lib/sv

%_prefix		%{vendor_prefix}
%_exec_prefix		%{vendor_exec_prefix}
%_var			%{vendor_var}
%_bindir		%{vendor_bindir}
%_sbindir		%{vendor_sbindir}
## RHEL 9+ defines %%dist_name in /usr/lib/rpm/macros.d/macros.dist,
## it breaks vendor packages without %%dist_name in its *.spec files.
## https://gitlab.osstech.co.jp/rpm/osstech-devel/-/issues/22
## "%%{?dist_ver:%%{?dist_name:/%%{dist_name}}}" is for backward compatibility
%_includedir		%{vendor_includedir}%{?pkg_name_base:/%{pkg_name_base}%{?pkg_ver_api}}%{!?pkg_name_base:%{?dist_ver:%{?dist_name:/%{dist_name}}}}
%_libdir		%{vendor_libdir}%{?pkg_name_base:/%{pkg_name_base}%{?pkg_ver_api}}%{!?pkg_name_base:%{?dist_ver:%{?dist_name:/%{dist_name}}}}
%_libexecdir		%{vendor_libexecdir}
%_sysconfdir		%{vendor_sysconfdir}
%_sharedstatedir	%{vendor_sharedstatedir}
%_localstatedir		%{vendor_localstatedir}
%_rundir		%{vendor_rundir}
%_vardir		%{vendor_vardir}
%_datadir		%{vendor_datadir}
%_infodir		%{vendor_infodir}
%_mandir		%{vendor_mandir}
%_docdir		%{vendor_docdir}
%_defaultdocdir		%{vendor_docdir}
%_svdir			%{vendor_svdir}

%_initddir		/etc/init.d
# Deprecated misspelling, present for backwards compatibility.
%_initrddir		%{_initddir}
%_pamddir		/etc/pam.d

## ----------------------------------------------------------------------

%with_vendor		1
%vendor_release		%{nil}
%vendor_rpmconfigdir	%{_rpmconfigdir}/%{vendor_id}
%vendor_rpmhome		%{vendor_rpmconfigdir}
%vendor_rpmmacros	%{vendor_rpmconfigdir}/macros

%name_prefix		%{vendor_name_prefix}
%ver_rel		%{?epoch:%{epoch}:}%{version}-%{release}

%platform_python	%{__python3}

## ======================================================================

%__perl                 /usr/bin/perl

%__ld                   %{vendor_bindir}/ld

%__clang                clang -fuse-ld=%{__ld}
%__clangxx              clang++ -fuse-ld=%{__ld}

%___vendor_build_pre \
eval "`env |sed -n 's/^\\([a-zA-Z0-9_]*\\)=.*$/unset \\1 || :/p' |grep -E -v ' (_|PWD|PATH|TERM|HOME|LOGNAME) '`"\
LC_ALL="C.UTF-8"\
PATH="%{vendor_bindir}:%{?___platform_path}%{!?___platform_path:/usr/sbin:/usr/bin:/sbin:/bin}"\
export LC_ALL PATH\
if \\\
  systemd-detect-virt --quiet --container 2>/dev/null \\\
  || ( \\\
    [[ -r /proc/self/uid_map ]] \\\
    && IFS=' ' read -r _ root_uid_mapped _ < /proc/self/uid_map \\\
    && [[ $root_uid_mapped -ne 0 ]] \\\
    ;\
  ) \\\
  || [[ -r /run/host/container-manager ]] \\\
  || [[ -r /run/.containerenv ]] \\\
  || [[ -r /.dockerenv ]] \\\
  || [[ -r /var/run/secrets/kubernetes.io/serviceaccount/token ]] \\\
  || [[ "$(< /proc/1/cgroup)" == *(lxc|docker|containerd)* ]] \\\
; then\
  export CONTAINER="${container:-true}"\
else\
  export HOME="%{_builddir}"\
fi\
%{___build_pre}\
CC="%{__cc}"\
CPP="%{__cpp}"\
CXX="%{__cxx}"\
CPPFLAGS="$CPPFLAGS"\
CFLAGS="${CFLAGS:-%optflags}"\
CXXFLAGS="${CXXFLAGS:-%optflags}"\
FFLAGS="${FFLAGS:-%optflags}"\
#LDFLAGS="-L/XXXX/lib -R/XXXX/lib${LDFLAGS:+ $LDFLAGS}"\
#PKG_CONFIG_PATH="/XXXX/lib/pkgconfig:/XXXX/share/pkgconfig:/usr/lib/pkgconfig${PKG_CONFIG_PATH:+:$PKG_CONFIG_PATH}"\
PERL="%{__perl}"\
export CC CPP CXX CPPFLAGS CFLAGS CXXFLAGS LDFLAGS PKG_CONFIG_PATH PERL\
%{?___platform_build_pre}\
%{nil}

%___build_template	#!%{___build_shell}\
%{___vendor_build_pre}\
%{nil}
%__spec_prep_pre	%{___vendor_build_pre}
%__spec_build_pre	%{___vendor_build_pre}
%__spec_install_pre	%{___vendor_build_pre}
%__spec_check_pre	%{___vendor_build_pre}
%__spec_clean_pre	%{___vendor_build_pre}

%__brp_check_rpaths	: %{_rpmconfigdir}/check-rpath is disabled.

%_install_script_path	%{vendor_bindir}:%{?___platform_path}%{!?___platform_path:/usr/sbin:/usr/bin:/sbin:/bin}

## Override %%__build_id_links not to generate /usr/lib/.build-id/*/*
%_build_id_links	none

%_debuginfo_subpackages 0

## ======================================================================

## RPM: Macros and SPEC to detect "from/to" package version in scriptlet
## https://gist.github.com/fumiyas/67b842325284c8adc186a6d7d541c656
##
## Example:
##
## %pre
## if %{vendor_ver_cmp_opposite < 1.2.3-222.new}; then
##   echo "Upgrade from $(%{vendor_ver_opposite}) (< 1.2.3-222.new)"
## fi
## exit 0
##
## %postun
## if %{vendor_ver_cmp_opposite < 1.2.3-222.new}; then
##   echo "Downgrade to $(%{vendor_ver_opposite}) (< 1.2.3-222.new)"
## fi
## exit 0
##
## NOTE: These macros cannot be used in %posttrans.

## In %pre, %post of the installing package on upgrade or downgrade:
##   Print version string of the uninstalling package
## In %preun, %postun of the uninstalling package on upgrade or downgrade:
##   Print version string of the installing package
%vendor_ver_opposite(n:) \
(\
  ver_current='%{?epoch:%{epoch}:}%{version}-%{release}'\
  LC_ALL=C \\\
  rpm \\\
    -q \\\
    --queryformat '%%|epoch?{%%{epoch}:}|%%{version}-%%{release}\\n' \\\
    %{!-n:%{name}}%{-n*} \\\
  |grep -v "^${ver_current//./\\\\.}$" \\\
  |grep -v ' is not installed$' \\\
  || :\\\
  ;\
)

## Compare 2 version strings with an operator
%vendor_ver_cmp() %{vendor_bindir}/rpm-version-cmp %{?1: "%{1}"}%{?2: "%{2}"}%{?3: "%{3}"}

## In %pre, %post on upgrade or downgrade:
##   Compare the uninstalling package version string and the specified one
## In %preun, %postun on upgrade or downgrade:
##   Compare the installing package version string and the specified one
%vendor_ver_cmp_opposite(n:) \
(\
  ver_opposite=$(%{expand:%%{vendor_ver_opposite %{-n:-n%{-n*}}}})\
  [[ -z $ver_opposite ]] && exit 1\
  %{expand:%%{vendor_ver_cmp $ver_opposite %1 %2}}\
)

## ======================================================================

%vendor_package \
Vendor: %{vendor_info}\
Requires(pre): %{vendor_id}-base >= 4.0\
Provides: %{vendor_id}-package = 4.0\
%global __elf_provides %{vendor_rpmconfigdir}/elf-provides %%{_libdir} %{__elf_provides}\
%global __elf_requires %{vendor_rpmconfigdir}/elf-requires %%{_libdir} %{__elf_requires}\
%global __libsymlink_requires %{vendor_rpmconfigdir}/libsymlink-requires %%{_libdir} %{__libsymlink_requires}\
%global __pkgconfig_provides %{vendor_rpmconfigdir}/pkgconfig-deps %%{_libdir} %{__pkgconfig_provides}\
%global __pkgconfig_requires %{vendor_rpmconfigdir}/pkgconfig-deps %%{_libdir} %{__pkgconfig_requires}\
%{nil}

%vendor_build_env() \
[ -f "%{vendor_rpmconfigdir}/%{1}-build.env" ] && . "%{vendor_rpmconfigdir}/%{1}-build.env"\
%{nil}

## OSSTech GCC uses ld-wrapper script, that emulates SVR4 ld(1)
%vendor_pkg_config_path() \
PKG_CONFIG_PATH="%{vendor_libdir}/%{1}/pkgconfig${PKG_CONFIG_PATH:+:$PKG_CONFIG_PATH}"\
%ifos linux\
LDFLAGS="-Wl,-rpath,%{vendor_libdir}/%{1}${LDFLAGS:+ $LDFLAGS}"\
%else\
LDFLAGS="-Wl,-R%{vendor_libdir}/%{1}${LDFLAGS:+ $LDFLAGS}"\
%endif\
export PKG_CONFIG_PATH LDFLAGS\
%{nil}

## OSSTech GCC uses ld-wrapper script, that emulates SVR4 ld(1)
%vendor_build_flags() \
CPPFLAGS="-I%{vendor_includedir}/%{1}${CPPFLAGS:+ $CPPFLAGS}"\
%ifos linux\
LDFLAGS="-L%{vendor_libdir}/%{1} -Wl,-rpath,%{vendor_libdir}/%{1}${LDFLAGS:+ $LDFLAGS}"\
%else\
LDFLAGS="-L%{vendor_libdir}/%{1} -Wl,-R%{vendor_libdir}/%{1}${LDFLAGS:+ $LDFLAGS}"\
%endif\
export CPPFLAGS LDFLAGS\
## Create a temporary prefix directory in $RPM_BUILD_DIR for a library\
## and set the prefix location into $%%{1}_prefix variable.\
## Some configure scripts expect that *.h and *.so files for a required\
## library are located in %%{_prefix}/include and %%{_prefix}/lib, that is,\
## %%{_prefix}/include/%%{name} and %%{_prefix}/lib/%%{name} are not usable.\
## See also: OSSTech ticket:41\
_vendor_tmp="%{1}_prefix"\
_vendor_tmp="${_vendor_tmp//[^a-z_0-9]/_}"\
eval "${_vendor_tmp}='$RPM_BUILD_DIR/%{name}-%{version}-%{release}-buildrequires-prefix/%{1}'"\
eval "rm -rf \\"\\$${_vendor_tmp}\\""\
eval "mkdir -p \\"\\$${_vendor_tmp}\\""\
eval "ln -s '%{vendor_includedir}/%{1}%{?2}' \\"\\$${_vendor_tmp}/include\\""\
eval "ln -s '%{vendor_libdir}/%{1}%{?2}' \\"\\$${_vendor_tmp}/lib\\""\
unset _vendor_tmp\
[ -f "%{vendor_rpmconfigdir}/%{1}-build.env" ] && . "%{vendor_rpmconfigdir}/%{1}-build.env"\
%{expand:%%{?vendor_%{1}_build_flags}}\
%{nil}

## OSSTech GCC uses ld-wrapper script, that emulates SVR4 ld(1)
%vendor_ld_run_path() \
%ifos linux\
export LDFLAGS="-Wl,-rpath,%{vendor_libdir}/%{1}${LDFLAGS:+ $LDFLAGS}"\
%else\
export LDFLAGS="-Wl,-R%{vendor_libdir}/%{1}${LDFLAGS:+ $LDFLAGS}"\
%endif\
%{nil}

