#  Copyright (c) 2005-2008 Wind River Systems, Inc.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
# See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
# Package extra version flag
%_ldat_rel 3.0bg

#==============================================================================
# ---- filesystem macros.
#
%_usr			/usr
%_usrsrc		%{_usr}/src
%_var			/var

#==============================================================================
# ---- Build system path macros.
#
%__remsh		%{__rsh}

%__id_u			true
%_fixowner		true
%_fixgroup		true

# Directory to helper scripts
%_ldat_scripts	%{_usrlibrpm}/wrs/scripts

#	The directory where newly built binary packages will be written.
%_rpmdir		/usr/src/rpm


#	Configurable distribution information, same as Distribution: tag in a
#	specfile.
#
%distribution	Wind River Linux

#	Configurable distribution URL, same as DistURL: tag in a specfile.
#	The URL will be used to supply reliable information to tools like
#	rpmfind.
#
# Note: You should not configure with disturl (or build packages with
# the DistURL: tag) unless you are willing to supply content in a
# yet-to-be-determined format at the URL specified.
#
#%disturl

#	Configurable vendor information, same as Vendor: in a specfile.
#
%vendor	Wind River Systems


#==============================================================================
# ---- per-platform macros.
#	Macros that are specific to an individual platform. The values here
#	will be used if the per-platform macro file does not exist..
#
%_arch			arm
%_build_arch		arm
%_vendor		wrs
%_os			linux
%_gnu			-gnu
%_target_platform	@TARGET@

#
# Define a generic value for optflags. Normally overridden by per-target macros.
%optflags		%{_cflags}

#
# Define our standard configuration
%_enable_debug_packages 1
%_use_internal_dependency_generator 1
%_missing_doc_files_terminate_build    1
%_unpackaged_files_terminate_build    1

# Remove perl requirements as they are often wrong
%__perl_requires	%{nil}

#
# Build policy macros.
#
%__arch_install_post	%{nil}

%__os_install_post	\
	%{_buildshell} %{_ldat_scripts}/brp-compress \
	%{?_enable_build_production:%{_buildshell} %{_ldat_scripts}/brp-strip %{__strip} %{__objdump}} \
	%{?_enable_build_production:%{_buildshell} %{_ldat_scripts}/brp-strip-shared %{__strip} %{__objdump}} \
	%{?_enable_build_production:%{_buildshell} %{_ldat_scripts}/brp-strip-static-archive %{__strip} %{__objdump}} \
	%{?_enable_build_production:%{_buildshell} %{_ldat_scripts}/brp-strip-comment-note %{__strip} %{__objdump}} \
%{nil}

%__spec_install_pre	%{___build_pre}

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

%__spec_clean_body	%{nil}

# Arbitrary tag name for LDAT checksum
%_arbitrary_tags	Class:Track:Trackprog:Ldatoverallsumsrc:Ldatsumsrc:Ldatoverallsummeta:Ldatsummeta

#       Template for debug information sub-package.
%debug_package \
%ifnarch noarch \
%global __debug_package 1 \
%package debuginfo \
%{expand:%(if test -f %{_pkg_ldat_sum}; then cat %{_pkg_ldat_sum}; fi)} \
Summary: Debug information for package %{name} \
Group: Development/Debug \
%description debuginfo \
This package provides debug information for package %{name}. \
Debug information is useful when developing applications that use this \
package or when debugging this package. \
%files debuginfo -f %{name}-debugfiles.list \
%defattr(-,root,root) \
%endif \
%{nil}

%__debug_install_post   \
	%{_buildshell} %{_ldat_scripts}/find-debuginfo.sh %{name} %{_builddir}/%{?buildsubdir} %{_usrlibrpm}/debugedit %{__objcopy} \
	%{?_enable_build_debug:%{_buildshell} %{_ldat_scripts}/brp-check-debuginfo} \
%{nil}

#==============================================================================
# ---- Scriptlet template templates.
#	Global defaults used for building scriptlet templates.
#

%___build_pre	\
  RPM_SOURCE_DIR=\"%{u2p:%{_sourcedir}}\"\
  RPM_BUILD_DIR=\"%{u2p:%{_builddir}}\"\
  RPM_OPT_FLAGS=\"%{_cflags}\"\
  RPM_ARCH=\"%{_arch}\"\
  RPM_OS=\"%{_os}\"\
  export RPM_SOURCE_DIR RPM_BUILD_DIR RPM_OPT_FLAGS RPM_ARCH RPM_OS\
  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\
  %{?buildroot:RPM_BUILD_ROOT=\"%{u2p:%{buildroot}}\"\
  export RPM_BUILD_ROOT}\
  %{?_javaclasspath:CLASSPATH=\"%{_javaclasspath}\"\
  export CLASSPATH}\
  \
  %{verbose:set -x}%{!verbose:exec > /dev/null}\
  umask 022\
  cd %{u2p:%{_builddir}}\

# Is this something we want to do?
#__spec_install_shell	@FAKEROOT@ %{___build_shell}

#==============================================================================
# ---- configure macros.
#	Macro(s) slavishly copied from autoconf's config.status.
#
%_prefix		/usr
%_exec_prefix		%{_prefix}
%_bindir		%{_exec_prefix}/bin
%_sbindir		%{_exec_prefix}/sbin
%_libexecdir		%{_exec_prefix}/libexec
%_datadir		%{_prefix}/share
%_sysconfdir		/etc
%_sharedstatedir	%{_prefix}/com
%_localstatedir		/var
%_libdir		%{_exec_prefix}/%{_lib}
%_includedir		%{_prefix}/include
%_oldincludedir		/usr/include
%_infodir		/usr/share/info
%_mandir		/usr/share/man
%_initrddir		%{_sysconfdir}/rc.d/init.d

%_defaultdocdir		%{_usr}/share/doc

#==============================================================================
# ---- config.guess platform macros.
#	Macro(s) similar to the tokens used by configure.
#
%_build			i686-crosscompile-linux-gnu
%_build_alias		%{_host_alias}
%_build_cpu		%{_host_cpu}
%_build_vendor		%{_host_vendor}
%_build_os		%{_host_os}
%_host_alias		arm
%_host_cpu		arm
%_host_vendor		wrs
%_host_os		linux-gnu
%_target		%{_host}
%_target_alias		%{_host_alias}
%_target_cpu		%{_host_cpu}
%_target_vendor		%{_host_vendor}
%_target_os		%{_host_os}

#==============================================================================
# ---- specfile macros.
#	Macro(s) here can be used reliably for reproducible builds.
#	(Note: Above is the goal, below are the macros under development)
#

%_host_cross_include_dir /usr/include

%_host_build_include_dir /usr/include
%_host_build_lib_dir /usr/lib
%_host_build_lib64_dir /usr/lib64

%_host_ldflags %{expand:%(echo '' | sed -e 's#\$(HOST_BUILD_LIB_DIR)#%%{_host_build_lib_dir}#' | sed -e 's#\$(HOST_BUILD_LIB64_DIR)#%%{_host_build_lib64_dir}#')}

%_host_cc gcc
%_host_cc_wrapper \""/usr/bin/ccache  gcc\""
%_host_cxx g++
%_host_cxx_wrapper \""/usr/bin/ccache  g++\""
%_host_copt -march=i486 -mtune=i686
%_host_cflags %{expand:%(echo '-g -O2 $(HOST_COPT) -fomit-frame-pointer -fPIC -pipe ' | sed -e 's#\$(HOST_COPT)#%%{_host_copt}#')}
%_host_cxxflags %{expand:%(echo '-g -O2 $(HOST_COPT) -fPIC -pipe' | sed -e 's#\$(HOST_COPT)#%%{_host_copt}#')}

%configure_host \
	export CFLAGS="%{_host_cflags} -I%{_host_build_include_dir}" ; \\\
	export CXXFLAGS="%{_host_cxxflags}" ; \\\
	export CC="%{_host_cc_wrapper}" ; \\\
	export CXX="%{_host_cxx_wrapper}" ; \\\
	export LDFLAGS="%{_host_ldflags}" ; \\\
	export LD="ld"

%_cflags %{expand:%(echo '-g ${TARGET_COMMON_CFLAGS} -Os $(TARGET_COPT) -pipe' | sed -e 's#$(TARGET_COPT)#-fomit-frame-pointer#' -e 's#$(TARGET_CPROF)#-fno-omit-frame-pointer#' -e 's#$(TARGET_CDEBUG)#-fno-omit-frame-pointer#')}
%_cxxflags %{expand:%(echo '-g ${TARGET_COMMON_CFLAGS} -Os $(TARGET_CXXOPT) -pipe' | sed -e 's#$(TARGET_CXXOPT)#-fno-rtti -fno-implicit-templates#' -e 's#$(TARGET_CXXPROF)#-fno-omit-frame-pointer#' -e 's#$(TARGET_CXXDEBUG)#-fno-omit-frame-pointer#')}

%_target_endian %(echo 'LITTLE' | tr A-Z a-z)

%_target_cc_wrapper %{__cc}
%_target_cxx_wrapper %{__cxx}

%configure_target \
        export HOST_BUILD_LIB_DIR="%{_host_build_lib_dir}" ; \\\
        export HOST_BUILD_LIB64_DIR="%{_host_build_lib64_dir}" ; \\\
        export PKG_CCACHE="/usr/bin/ccache" ; \\\
        export PKG_DISTCC="" ; \\\
        export CXX="%{__cxx}" ; \\\
        export CC="%{__cc}" ;  \\\
        export AS="%{__as}" ; \\\
        export AR="%{__ar}" ; \\\
        export CPP="%{__cpp}" ; \\\
        export LD="%{__cc}" ; \\\
        export LDFLAGS="" ; \\\
        export NM="%{__nm}" ; \\\
        export OBJCOPY="%{__objcopy}" ; \\\
        export OBJDUMP="%{__objdump}" ; \\\
        export RANLIB="%{__ranlib}" ; \\\
        export READELF="%{__readelf}" ; \\\
        export CFLAGS="`echo %{_cflags}`" ; \\\
        export CXXFLAGS="`echo %{_cxxflags}`" ; \\\
        export STRIP="%{__strip}" ; \\\
	%{?_config_site:export CONFIG_SITE="%{_config_site}"}

%configure \
  ./configure --host=%{_host} --build=%{_build} \\\
	--program-prefix=%{?_program_prefix} \\\
 	--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} \\\
	%{?_config_cache:--cache-file=%{_config_cache}}

# Helper script for setting up initscripts
%_chkconfig_sh() %{_buildshell} %{_ldat_scripts}/chkconfig.sh %{buildroot}%{_initrddir}/%{1} %{buildroot}/etc/rc.d

#
# Override the normal spec file environment
#
%prep \
%%prep\
LANG=C\
export LANG\
unset DISPLAY\
%{nil}

%build %%build\
LANG=C\
export LANG\
unset DISPLAY\
%{nil}

%install %{?_enable_debug_packages:%{debug_package}}\
%%install\
LANG=C\
export LANG\
unset DISPLAY\
%{nil}

# Resolve multilib sysroot header files conflicts
%_multilib_header_install() %{__spec_install_shell} %{_ldat_scripts}/multilib_header_install.sh %{_ldat_scripts} %{1} %{2} %{3}

# Disable the %clean step
%clean \
%%clean\
exit 0

# Disable %check step
%check \
%%check\
exit 0

# Add our own %if_arch to compare _arch instead of _target_cpu
%if_arch() %{expand:%%if %(rc=0 ; for each in %{*}; do if [ "$each" = "%{_arch}" ]; then rc=1 ; break ; fi ; done ; echo $rc) }

# Convenient macro to check if static linking should be enabled
%if_static() %{expand:%%if %(if [ "staticlink" = "%{_target_scaling}" ]; then echo 1; else echo 0; fi ) }

%__spec_clean_template #!%{__spec_clean_shell}\
%{__spec_clean_pre}\
%{?_ldat_clean}\
%{nil}
