#!/usr/bin/bash
# A script to quickly create .rpmmacro setting for a specific machine environment
orig_build_cflags=$(rpm --eval %build_cflags)
orig_build_cxxflags=$(rpm --eval %build_cxxflags)
orig_build_fflags=$(rpm --eval %build_fflags)
orig_build_ldflags=$(rpm --eval %build_ldflags)
orig_dist=$(rpm --eval %dist)
sections_options="%{?call_graph:%{?pgo:-ffunction-sections -fdata-sections}}"

build_cflags="$orig_build_cflags $sections_options"
build_cxxflags="$orig_build_cxxflags $sections_options"
build_fflags="$orig_build_fflags $sections_options"
build_ldflags="$orig_build_ldflags %{_hardened_ldflags} %{?call_graph:%{?pgo:-Wl,\"--section-ordering-file,%{__global_link_order}\"}}"
dist="$orig_dist%{?pgo:_pgo}"

echo "# This file placed in ~/.rpmmacros will provided the need changes to use"
echo "# sediment code layout optimizations as part of the rpm build process."

echo "%build_cflags $build_cflags"
echo "%build_cxxflags $build_cxxflags"
echo "%build_fflags $build_fflags"
echo "%__global_link_order \\\"%{u2p:%{_builddir}}/%{name}-%{version}-%{release}.order\\\""
echo "%build_ldflags $build_ldflags"
echo "%dist $dist"

cat <<EOT

%___build_pre_env \\
  RPM_SOURCE_DIR=\"%{_sourcedir}\"\\
  RPM_BUILD_DIR=\"%{builddir}\"\\
  RPM_OPT_FLAGS=\"%{optflags}\"\\
  RPM_LD_FLAGS=\"%{?build_ldflags}\"\\
  RPM_ARCH=\"%{_arch}\"\\
  RPM_OS=\"%{_os}\"\\
  RPM_BUILD_NCPUS=\"%{_smp_build_ncpus}\"\\
  RPM_SPECPARTS_DIR=\"%{specpartsdir}\"\\
  export RPM_SOURCE_DIR RPM_BUILD_DIR RPM_OPT_FLAGS RPM_ARCH RPM_OS RPM_BUILD_NCPUS RPM_SPECPARTS_DIR RPM_LD_FLAGS\\
  RPM_DOC_DIR=\"%{_docdir}\"\\
  export RPM_DOC_DIR\\
  RPM_PACKAGE_NAME=\"%{NAME}\"\\
  RPM_PACKAGE_VERSION=\"%{VERSION}\"\\
  RPM_PACKAGE_RELEASE=\"%{RELEASE}\"\\
  export RPM_PACKAGE_NAME RPM_PACKAGE_VERSION RPM_PACKAGE_RELEASE\\
  LANG=C.UTF-8\\
  export LANG\\
  unset CDPATH DISPLAY ||:\\
  unset DEBUGINFOD_URLS ||:\\
  unset RPM_CONFIG_DIR ||:\\
  RPM_BUILD_ROOT=\"%{buildroot}\"\\
  export RPM_BUILD_ROOT\\
  %{?_javaclasspath:CLASSPATH=\"%{_javaclasspath}\"\\
  export CLASSPATH}\\
  PKG_CONFIG_PATH=\"\${PKG_CONFIG_PATH}:%{_libdir}/pkgconfig:%{_datadir}/pkgconfig\"\\
  export PKG_CONFIG_PATH\\
  CONFIG_SITE=\${CONFIG_SITE:-NONE}\\
  export CONFIG_SITE \\
  %{?call_graph:%{?pgo: gv2link < %{call_graph} > %{__global_link_order}  } }
EOT
