# vim: syntax=spec
python_basename_xen=${python_basename_xen}
python_basename_qemuu=${python_basename_qemuu}
python_basename_ovmf=${python_basename_ovmf}

test -n "${python_basename_xen}"
test -n "${python_basename_qemuu}"

xen_python=
qemuu_python=
ovmf_python=
case "${python_basename_qemuu}" in
	python3)
		test -x "$(type -P python3.6)" && qemuu_python="$_"
	;;
esac
case "${python_basename_ovmf}" in
	python3)
		test -x "$(type -P python3.6)" && ovmf_python="$_"
	;;
esac
test -n "${xen_python}" || xen_python="$(type -P ${python_basename_xen} | xargs readlink -f)"
test -n "${qemuu_python}" || qemuu_python="$(type -P ${python_basename_qemuu} | xargs readlink -f)"
#
tar xfva %{SOURCE1000}
tee .scmversion <<'_EOV_'
%{xen_date_commit} git:%{xen_revision_short}
_EOV_
pushd tools/firmware/seabios-dir-remote
tee .version <<'_EOV_'
%{seabios_revision_full}
_EOV_
popd
pushd tools/firmware/etherboot
ls -al
tar cfa ipxe.tar.gz ipxe
popd
# we control the version info of this package
# to gain control of filename of xen.gz
XEN_VERSION=%{version}
XEN_VERSION=${XEN_VERSION%%%%.*}
XEN_SUBVERSION=%{version}
XEN_SUBVERSION=${XEN_SUBVERSION#*.}
XEN_SUBVERSION=${XEN_SUBVERSION%%%%.*}
XEN_EXTRAVERSION="%version-%release"
XEN_EXTRAVERSION="${XEN_EXTRAVERSION#*.}"
XEN_EXTRAVERSION="${XEN_EXTRAVERSION#*.}"
# remove trailing B_CNT to reduce build-compare noise
XEN_EXTRAVERSION="${XEN_EXTRAVERSION%%.*}"
XEN_FULLVERSION="$XEN_VERSION.$XEN_SUBVERSION.$XEN_EXTRAVERSION"
tee xen/xen-version  <<_EOF_
export XEN_VERSION=$XEN_VERSION
export XEN_SUBVERSION=$XEN_SUBVERSION
export XEN_EXTRAVERSION=.$XEN_EXTRAVERSION
export XEN_FULLVERSION=$XEN_FULLVERSION
_EOF_

%if 0%{?__debug_package}
debug_symbols=y
qemuu_extra_args="--enable-debug-info"
%else
debug_symbols=n
qemuu_extra_args="--disable-debug-info"
%endif
if test -n "${qemuu_python}"
then
	qemuu_extra_args="${qemuu_extra_args} --python=${qemuu_python}"
fi
XEN_DOMAIN="` echo '%_project' | sed 's@^home:@@'`"
test -x "$(type -p gcc)"     && CC=$_
test -x "$(type -p gcc-4.6)" && CC=$_
test -x "$(type -p gcc-4.7)" && CC=$_
test -x "$(type -p gcc-4.8)" && CC=$_
test -x "$(type -p gcc-7)"   && CC=$_
test -x "$(type -p gcc-8)"   && CC=$_
test -x "$(type -p gcc-9)"   && CC=$_
test -x "$(type -p gcc-10)"  && CC=$_
test -x "$(type -p cpp)"     && CPP=$_
test -x "$(type -p cpp-4.6)" && CPP=$_
test -x "$(type -p cpp-4.7)" && CPP=$_
test -x "$(type -p cpp-4.8)" && CPP=$_
test -x "$(type -p cpp-7)"   && CPP=$_
test -x "$(type -p cpp-8)"   && CPP=$_
test -x "$(type -p cpp-9)"   && CPP=$_
test -x "$(type -p cpp-10)"  && CPP=$_
test -x "$(type -p g++)"     && CXX=$_
test -x "$(type -p g++-4.6)" && CXX=$_
test -x "$(type -p g++-4.7)" && CXX=$_
test -x "$(type -p g++-4.8)" && CXX=$_
test -x "$(type -p g++-7)"   && CXX=$_
test -x "$(type -p g++-8)"   && CXX=$_
test -x "$(type -p g++-9)"   && CXX=$_
test -x "$(type -p g++-10)"  && CXX=$_
tee .env.sh <<_EOF_
debug_symbols=${debug_symbols}
export CC="`readlink -f $CC`"
export CPP="`readlink -f $CPP`"
export CXX="`readlink -f $CXX`"
export XEN_DOMAIN=${XEN_DOMAIN}
export XEN_BUILD_HOST=host
export XEN_BUILD_DATE="`date -ud '@%{xen_date_unix}'`"
export XEN_BUILD_TIME="`date -ud '@%{xen_date_unix}' +%%T`"
export SMBIOS_REL_DATE="`date -ud '@%{xen_date_unix}' +%%m/%%d/%%Y`"
export VGABIOS_REL_DATE="`date -ud '@%{xen_date_unix}' '+%%d %%b %%Y'`"
export EXTRA_CFLAGS_XEN_TOOLS='%{optflags}'
export EXTRA_CFLAGS_QEMU_TRADITIONAL='%{optflags}'
export EXTRA_CFLAGS_QEMU_XEN='%{optflags}'
export WGET=$(type -P false)
export GIT=$(type -P false)
export PYTHON=$(type -P "${xen_python}" | xargs --no-run-if-empty readlink -f)
export OVMF_PYTHON_COMMAND=$(type -P "${ovmf_python}" | xargs --no-run-if-empty readlink -f)
XEN_BOOT_DIR=/boot
%if 0%{?suse_version} < 1500
XEN_EFI_DIR=%{_libdir}/efi
%else
XEN_EFI_DIR=%{_datadir}/efi/%{_target_cpu}
%endif
XEN_DEBUG_DIR=%{_libexecdir}/%{name}/debug
_EOF_
. ./.env.sh
if test -n "${qemuu_python}"
then
	find tools/qemu-xen-dir-remote -type f -exec grep -lZ '%{_bindir}/python$' '{}' + | sort -z | tr '\0' '\n' | while read
	do
		i="${REPLY}"
		sed -i~ "
		s@%{_bindir}/python[.0-9]*@${qemuu_python}@
		" "$i"
		diff -u "$i"~ "$i" && exit 1
		touch --reference="$i"~ "$i"
		rm -fv "$i"~
	done
	find tools/qemu-xen-dir-remote -type f -exec grep -lZ '%{_bindir}/env python' '{}' + | sort -z | tr '\0' '\n' | while read
	do
		i="${REPLY}"
		sed -i~ "
		s@%{_bindir}/env python[.0-9]*@${qemuu_python}@
		" "$i"
		diff -u "$i"~ "$i" && exit 1
		touch --reference="$i"~ "$i"
		rm -fv "$i"~
	done
fi
if test -n "${ovmf_python}"
then
	find tools/firmware/ovmf-dir-remote -type f -exec grep -lZ '%{_bindir}/python$' '{}' + | sort -z | tr '\0' '\n' | while read
	do
		i="${REPLY}"
		sed -i~ "
		s@%{_bindir}/python[.0-9]*@${ovmf_python}@
		" "$i"
		diff -u "$i"~ "$i" && exit 1
		touch --reference="$i"~ "$i"
		rm -fv "$i"~
	done
	find tools/firmware/ovmf-dir-remote -type f -exec grep -lZ '%{_bindir}/env python' '{}' + | sort -z | tr '\0' '\n' | while read
	do
		i="${REPLY}"
		sed -i~ "
		s@%{_bindir}/env python[.0-9]*@${ovmf_python}@
		" "$i"
		diff -u "$i"~ "$i" && exit 1
		touch --reference="$i"~ "$i"
		rm -fv "$i"~
	done
fi
find * -type f -exec grep -lZ '%{_bindir}/python$' '{}' + | sort -z | tr '\0' '\n' | while read
do
	i="${REPLY}"
	sed -i~ "
	s@%{_bindir}/python[.0-9]*@$PYTHON@
	" "$i"
	diff -u "$i"~ "$i" && exit 1
	touch --reference="$i"~ "$i"
	rm -fv "$i"~
done
find * -type f -exec grep -lZ '%{_bindir}/env python' '{}' + | sort -z | tr '\0' '\n' | while read
do
	i="${REPLY}"
	sed -i~ "
	s@%{_bindir}/env python[.0-9]*@$PYTHON@
	" "$i"
	diff -u "$i"~ "$i" && exit 1
	touch --reference="$i"~ "$i"
	rm -fv "$i"~
done
sed -i~ '
s@^#define _CONRING_SIZE 16384@#define _CONRING_SIZE (64*1024)@
' xen/drivers/char/console.c
diff -u "$_"~ "$_" && exit 1
create_build_xen()
{
tee build_xen.sh <<-EOF
#!/bin/bash
echo "+ running '\$0' '\$*'"
set -ex
time \
make \
	BOOT_DIR=${XEN_BOOT_DIR} \
	EFI_DIR=${XEN_EFI_DIR} \
	DEBUG_DIR=${XEN_DEBUG_DIR} \
	DESTDIR=%{buildroot} \
	XEN_WHOAMI="debug=${CONFIG_DEBUG}" \
	CC=$CC \
	CXX=$CXX \
	HOSTCC=$CC \
	HOSTCXX=$CXX \
	HOST_CC=$CC \
	%{?_smp_mflags} \
	-k \
	$@ \
	"\$@"
EOF
cat build_xen.sh
}
#
cp -al . ../non-dbg
cp -al . ../dbg
mv -t . ../non-dbg ../dbg
#
%if 0%{with_hypervisor}
if test "$debug_symbols" = "y"
then
	pushd dbg
	CONFIG_DEBUG=y
	if test -e xen/tools/kconfig
	then
		rm -fv xen/.config
		{
		echo CONFIG_DEBUG=${CONFIG_DEBUG}
		echo CONFIG_DEBUG_INFO=y
		echo CONFIG_NR_CPUS=512
		} > xen/.config
		yes '' | make -C xen oldconfig HOSTCC=$CC
		create_build_xen
	else
		create_build_xen debug_symbols=${CONFIG_DEBUG}
	fi
	bash build_xen.sh xen
	popd
fi
%endif
#
pushd non-dbg
bash -x autogen.sh
%configure \
	--enable-debug \
	--disable-docs \
	--disable-ocamltools \
	--disable-ovmf \
	--disable-qemu-traditional \
	--disable-pvshim \
	--disable-stubdom \
	--disable-xen \
	--with-debugdir=${XEN_DEBUG_DIR} \
	'--with-rundir=%{_rundir}' \
	'--docdir=%{_defaultdocdir}/%{name}' \
	--with-extra-qemuu-configure-args="${qemuu_extra_args}" \
%ifarch x86_64
%if %{with xen_ovmf}
	--enable-ovmf \
%endif
%endif
%if %{with xen_docs}
	--enable-docs \
%endif
%if %{with xen_qemu_trad}
	--enable-qemu-traditional \
%endif
	--enable-tools \
%if %{with xen_systemd}
	--enable-systemd \
	'--with-systemd=%{_unitdir}' \
	'--with-systemd-modules-load=%{_unitdir}' \
%else
	--disable-systemd \
	'--with-initddir=%{_initddir}' \
%endif
%if %{with xen_stubdom}
	--enable-stubdom \
%if %{with xen_qemu_trad}
	--enable-ioemu-stubdom \
%else
	--disable-ioemu-stubdom \
%endif
	--disable-c-stubdom \
	--disable-caml-stubdom \
	--disable-pv-grub \
	--enable-xenstore-stubdom \
	--disable-vtpm-stubdom \
	--disable-vtpmmgr-stubdom \
%endif
	%{nil}
unset CFLAGS
unset CXXFLAGS
unset FFLAGS
unset LDFLAGS
make %{?_smp_mflags} -k -C tools/include/xen-foreign HOSTCC=$CC
export EXTRA_OPTFLAGS='-fPIC'
make %{?_smp_mflags} \
	"CC=$CC" \
	"CXX=$CXX" \
	"CPP=$CC -E" \
	"SEABIOS_CPP=$CPP" \
	"GCC=$CC" \
	"HOSTCC=$CC" \
	"HOSTCXX=$CXX" \
	"HOST_CC=$CC" \
	"BUILD_CC=$CC" \
	"BUILD_CXX=$CXX" \
	debug_symbols=$debug_symbols \
	-k

#
%if %{with xen_kmp}
# pv driver modules
export XL=/usr/src/linux
export XEN=/usr/src/linux/include/xen
mkdir -vp obj
for flavor in %flavors_to_build; do
    rm -rf obj/$flavor
    cp -r unmodified_drivers/linux-2.6 obj/$flavor
    cd obj/$flavor
    sh mkbuildtree
    make -C /usr/src/linux-obj/%_target_cpu/$flavor modules \
	%{?_smp_mflags} -k \
        M=$PWD
    cd ../..
done
# pv driver modules
%endif
#
%if 0%{with_hypervisor}
CONFIG_DEBUG=n
if test -e xen/tools/kconfig
then
	rm -fv xen/.config
	{
	echo CONFIG_DEBUG=${CONFIG_DEBUG}
	echo CONFIG_DEBUG_INFO=y
	echo CONFIG_NR_CPUS=512
	} > xen/.config
	yes '' | make -C xen oldconfig HOSTCC=$CC
	create_build_xen
else
	create_build_xen debug_symbols=${CONFIG_DEBUG}
fi
bash build_xen.sh xen
%endif
