#------------------------------------------------------------------------------
# Vine defaults
#
%_topdir	%{getenv:HOME}/rpm

# Distribution name
%distribution	Vine Linux

# default source dir
%_sourcedir %{_topdir}/SOURCES/%{name}

# default dirs
%_sharedstatedir	/var/lib
%_localstatedir		/var

# additional dirs
%_sysbindir		/bin
%_rootbindir		/bin
%_syssbindir		/sbin
%_rootsbindir		/sbin
%_syslibdir		/%{_lib}
%_rootlibdir		/%{_lib}
%_localedir		%{_datadir}/locale
%_initdir		%{_sysconfdir}/rc.d/init.d
%_vinedir		%{_prefix}/vine

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

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

#------------------------------------------------------------------------------
# arch macro for all Intel i?86 compatibile processors
#
%ix86   i386 i486 i586 i686 pentium3 pentium4 athlon geode k6 k7
 
#------------------------------------------------------------------------
# Compat32 build arch
#
%_compat32_arch i686

#------------------------------------------------------------------------
# _dist_release
#
%_dist_release vl%(cat /etc/vine-release | cut -f3 -d" " | cut -f1 -d.)
%dist vl%(cat /etc/vine-release | cut -f3 -d" " | cut -f1 -d.)

#------------------------------------------------------------------------
# Useful ruby macros like perl's above (from Akira Yamada <akira@arika.org>)
# OBSOLETE, please use /etc/rpm/macros.ruby{,gems}
#
%ruby     /usr/bin/ruby
%__ruby   /usr/bin/ruby
%rbindir  %(%{ruby} -r rbconfig -e 'print RbConfig::CONFIG["bindir"]')
%rlibdir  %(%{ruby} -r rbconfig -e 'print RbConfig::CONFIG["rubylibdir"]')
%rarchdir %(%{ruby} -r rbconfig -e 'print RbConfig::CONFIG["archdir"]')
%rslibdir %(%{ruby} -r rbconfig -e 'print RbConfig::CONFIG["sitelibdir"]')
%rsarchdir %(%{ruby} -r rbconfig -e 'print RbConfig::CONFIG["sitearchdir"]')

#------------------------------------------------------------------------
# Macros for emacsen-common (from Kazuhisa Takei <takei@vinelinux.org>)
#
%emacsen_commondir /usr/lib/emacsen-common
%emacsen_pkgdir %{emacsen_commondir}/packages
%_installemacsenscript(%1,%2) %{expand:
  install -m755 %2 $RPM_BUILD_ROOT/usr/lib/emacsen-common/packages/install/%1
}
%_removeemacsenscript(%1,%2) %{expand: 
  install -m755 %2 $RPM_BUILD_ROOT/usr/lib/emacsen-common/packages/remove/%1 
}
%_emacsenPackageInstall(%1) %{expand: 
  /usr/lib/emacsen-common/emacs-package-install %1
}
%_emacsenPackageRemove(%1) %{expand: 
  /usr/lib/emacsen-common/emacs-package-remove %1
}
%_addemacsenlist(%1) %{expand: 
  /usr/lib/emacsen-common/generate-package-list %1
}
%_removeemacsenlist(%1) %{expand: 
  /usr/lib/emacsen-common/remove-package-list %1
}
%_addflavorslist(%1) %{expand: 
  /usr/lib/emacsen-common/generate-flavors-list %1
}
%_removeflavorslist(%1) %{expand: 
  /usr/lib/emacsen-common/remove-flavors-list %1
}
%_installemacs(%1) %{expand: 
  /usr/lib/emacsen-common/emacs-install %1
}
%_removeemacs(%1) %{expand: 
  /usr/lib/emacsen-common/emacs-remove %1
}

#------------------------------------------------------------------------
# Macros for font package
#
%__mkfontdir     umask 133;/usr/bin/mkfontdir
%__mkfontscale   umask 133;/usr/bin/mkfontscale
%__fccache       umask 133;which fc-cache &> /dev/null && fc-cache -f
%__updmap        [ -x %{_bindir}/updmap-sys ] && %{_bindir}/env - %{_bindir}/updmap-sys --nostop 2> /dev/null
%__upddeffont    [ -x %{_sbindir}/update-defaultfont ] && %{_sbindir}/update-defaultfont 2> /dev/null
%__mktcapdir umask 133;which mktcapdir &> /dev/null && mktcapdir
%__font_install_post(%1)	 \
 ( ( [ -f "%1/*.ttfconf" ] && %{__mktcapdir} %1 %1/*.ttfconf ); \
   %{__upddeffont} ; \
   %{__updmap} ; \
   %{__fccache} %1 ) >& /dev/null ||:
%__font_install_postun(%1)	\
 ( ( [ -f "%1/*.ttfconf" ] && %{__mktcapdir} %1 %1/*.ttfconf ); \
   %{__upddeffont} ; \
   %{__updmap} ; \
   %{__fccache} %1 ) >& /dev/null ||:

#------------------------------------------------------------------------
# compiler flags

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

# LTO is the default
#   "%define _lto_cflags %{nil}"  to opt out
#
# We currently have -ffat-lto-objects turned on out of an abundance of
# caution.  To remove it we need to do a check of the installed .o/.a files
# to verify they have real sections/symbols after LTO stripping.  That
# way we can detect installing an unusable .o/.a file.  This is on the TODO
# list for F34.
%_gcc_lto_cflags	-flto=auto -ffat-lto-objects
%_clang_lto_cflags	-flto=thin -ffat-lto-objects
%_lto_cflags		%{expand:%%{_%{toolchain}_lto_cflags}}

# Some linkers default to a build-id algorithm that is not supported by rpmbuild,
# so we need to specify the right algorithm to use.
%_build_id_flags	-Wl,--build-id=sha1

%_general_options       -O2 %{?_lto_cflags} -fexceptions -g -grecord-gcc-switches -pipe
%_warning_options       -Wall -Werror=format-security
%_preprocessor_defines  -Wp,-D_FORTIFY_SOURCE=2 -Wp,-D_GLIBCXX_ASSERTIONS

# Common variables are no longer generated by default by gcc and clang
# If they are needed then add "%define _legacy_common_support 1" to the spec file.
%_legacy_options	%{?_legacy_common_support: -fcommon}

%__global_compiler_flags %{?_general_options} %{?_warning_options} %{?_preprocessor_defines} %{?_legacy_options}

# C compiler flags.  This is traditionally called CFLAGS in makefiles.
# Historically also available as %%{optflags}, and %%build sets the
# environment variable RPM_OPT_FLAGS to this value.
%build_cflags %{optflags}

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

# Fortran compiler flags.  Makefiles use both FFLAGS and FCFLAGS as
# the corresponding variable names.
%build_fflags %{optflags} -I%{_fmoddir}

# Vala compiler flags. This is used to set VALAFLAGS.
%build_valaflags -g

# When clang is used as a linker driver, it does not auto-detect the LTO
# bytecode and neither does bfd, so we need to explicitly pass the -flto
# flag when linking.
%_clang_extra_ldflags %{?_lto_cflags}

# Link editor flags.  This is usually called LDFLAGS in makefiles.
# (Some makefiles use LFLAGS instead.)  The default value assumes that
# the flags, while intended for ld, are still passed through the gcc
# compiler driver.  At the beginning of %%build, the environment
# variable RPM_LD_FLAGS to this value.
# When clang is used as a linker driver, it does not auto-detect the LTO
# bytecode and neither does bfd, so we need to explicitly pass the -flto
# flag when linking.
%build_ldflags -Wl,-z,relro %{?_ld_as_needed_flags} %{?_ld_symbols_flags} %{?_hardened_ldflags} %[ "%{toolchain}" == "clang" ? "%{?_clang_extra_ldflags}" : "" ] %{_build_id_flags} %{?_package_note_flags} %{?_distro_extra_ldflags}

# Expands to shell code to set the compiler/linker environment
# variables CFLAGS, CXXFLAGS, FFLAGS, FCFLAGS, VALAFLAGS, 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.
# RUSTFLAGS is only set when %%{build_rustflags} is available.
%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 ; \
  VALAFLAGS="${VALAFLAGS:-%{build_valaflags}}" ; export VALAFLAGS ;%{?build_rustflags:
  RUSTFLAGS="${RUSTFLAGS:-%{build_rustflags}}" ; export RUSTFLAGS ;} \
  LDFLAGS="${LDFLAGS:-%{build_ldflags}}" ; export LDFLAGS ; \
  CC="${CC:-%{__cc}}" ; export CC ; \
  CXX="${CXX:-%{__cxx}}" ; export CXX

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

#------------------------------------------------------------------------
# Architecture-specific support.  Internal.  Do not use directly.
#

#%__cflags_arch_x86_64 %["%{toolchain}" == "gcc" ? "-march=x86-64-v2" : ""]
#%__cflags_arch_x86_64 %{nil}
%__cflags_arch_x86_64 -march=x86-64-v2

#------------------------------------------------------------------------
# stripping static libraries with LTO.
#

%__brp_strip_lto /usr/lib/rpm/brp-strip-lto %{__strip}

%__os_install_post    \
    %{?__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} \
    %{?__brp_remove_la_files} \
    %{?__os_install_post_python} \
%{nil}
