# vim:filetype=spec
# Modules always contain just 32-bit code
%global _libdir %{_exec_prefix}/lib
%global _binaries_in_noarch_packages_terminate_build 0
#%%undefine _missing_build_ids_terminate_build
%{expand:%%{!?buildsubdir:%%global buildsubdir grub-%{tarversion}}}
%{expand:%%{!?_licensedir:%%global license %%%%doc}}

%if %{?_with_ccache: 1}%{?!_with_ccache: 0}
%global cc_equals CC=/usr/%{_lib}/ccache/gcc
%else
%global cc_equals %{nil}
%endif

%global efi_cflags %(echo %{optflags} | 			\\\
	sed							\\\
		-e 's/-O./-g3/g'				\\\
		-e 's/-fstack-protector[[:alpha:]-]\\+//g'	\\\
		-e 's/-Wp,-D_FORTIFY_SOURCE=[[:digit:]]\\+//g'	\\\
		-e 's/--param=ssp-buffer-size=4//g'		\\\
		-e 's/-mregparm=3/-mregparm=4/g'		\\\
		-e 's/-fexceptions//g'				\\\
		-e 's/-fasynchronous-unwind-tables//g'		\\\
		-e 's/^/ -fno-strict-aliasing /' ) %{nil}
#%global legacy_cflags %(echo %{efi_cflags} |			\\\
#	sed							\\\
#		-e 's/-m64//g'					\\\
#		-e 's/-mcpu=power[[:alnum:]]\\+/-mcpu=power6/g'	\\\
#		) %{nil}
%global legacy_cflags -Os -g3 -mcpu=power6 -fno-strict-aliasing


%global with_efi_arch 0
%global with_alt_efi_arch 0
%global with_legacy_arch 0
%global grubefiarch %{nil}
%global grublegacyarch %{nil}

# sparc is always compiled 64 bit
%ifarch %{sparc}
%global target_cpu_name sparc64
%global _target_platform %{target_cpu_name}-%{_vendor}-%{_target_os}%{?_gnu}
%global legacy_target_cpu_name %{_arch}
%global legacy_package_arch ieee1275
%global platform ieee1275
%endif
# ppc is always compiled 64 bit
%ifarch ppc ppc64 ppc64le
%global target_cpu_name %{_arch}
%global legacy_target_cpu_name powerpc
%global legacy_package_arch %{_arch}
%global legacy_grub_dir powerpc-ieee1275
%global _target_platform %{target_cpu_name}-%{_vendor}-%{_target_os}%{?_gnu}
%global platform ieee1275
%endif

%global efi_only aarch64 %{arm}
%global efi_arch x86_64 ia64 %{efi_only}
%ifarch %{efi_arch}
%global with_efi_arch 1
%else
%global with_efi_arch 0
%endif
%ifarch %{efi_only}
%global with_efi_only 1
%else
%global with_efi_only 0
%endif
%{!?with_efi_arch:%global without_efi_arch 0}
%{?with_efi_arch:%global without_efi_arch 1}
%{!?with_efi_only:%global without_efi_only 0}
%{?with_efi_only:%global without_efi_only 1}

### fixme
%ifarch aarch64 %{arm}
%global efi_modules " http linux "
%else
%global efi_modules " backtrace http linuxefi usb usbserial_common usbserial_pl2303 usbserial_ftdi usbserial_usbdebug "
%endif

%ifarch x86_64
%global efiarch x64
%global target_cpu_name %{_arch}
%global grub_target_name %{_arch}-efi
%global package_arch efi-x64

%global legacy_target_cpu_name i386
%global legacy_package_arch pc
%global platform pc

%global alt_efi_arch ia32
%global alt_target_cpu_name i386
%global alt_grub_target_name i386-efi
%global alt_platform efi
%global alt_package_arch efi-ia32
%global alt_efi_cflags %(echo %{efi_cflags} |			\\\
	sed							\\\
		-e 's/-m64//g'				\\\
	) %{nil}
%endif

%ifarch aarch64
%global efiarch aa64
%global target_cpu_name aarch64
%global grub_target_name arm64-efi
%global package_arch efi-aa64
%endif

%ifarch %{arm}
%global efiarch arm
%global target_cpu_name arm
%global grub_target_name arm-efi
%global package_arch efi-arm
%endif

%global _target_platform %{target_cpu_name}-%{_vendor}-%{_target_os}%{?_gnu}
%global _alt_target_platform %{alt_target_cpu_name}-%{_vendor}-%{_target_os}%{?_gnu}

%ifarch %{efi_arch}
%global with_efi_arch 1
%global grubefiname grub%{efiarch}.efi
%global grubeficdname gcd%{efiarch}.efi
%global grubefiarch %{target_cpu_name}-efi
%endif

%if 0%{?alt_efi_arch:1}
%global with_alt_efi_arch 1
%global grubaltefiname grub%{alt_efi_arch}.efi
%global grubalteficdname gcd%{alt_efi_arch}.efi
%global grubaltefiarch %{alt_target_cpu_name}-efi
%endif

# Figure out the right file path to use
%global efidir %(eval echo $(grep ^ID= /etc/os-release | sed -e 's/^ID=//' -e 's/rhel/redhat/'))

%ifnarch %{efi_only}
%global with_legacy_arch 1
%global grublegacyarch %{legacy_target_cpu_name}-%{platform}
%endif

%global evr %{epoch}:%{version}-%{release}

%if %{with_legacy_arch}
%global with_efi_common 0
%global with_legacy_common 1
%else
%global with_efi_common 1
%global with_legacy_common 0
%endif

%if 0%{with_efi_common}
%global common_srcdir grub-%{grubefiarch}-%{tarversion}
%endif
%if 0%{with_legacy_common}
%global common_srcdir grub-%{grublegacyarch}-%{tarversion}
%endif

%define define_legacy_variant()						\
%{expand:%%package %{1}}						\
Summary:	Bootloader with support for Linux, Multiboot, and more	\
Group:		System Environment/Base					\
Requires:	%{name}-common = %{evr}					\
Requires:	%{name}-tools-minimal = %{evr}				\
Requires:	%{name}-%{1}-modules = %{evr}				\
Requires:	gettext which file					\
Requires:	%{name}-tools-extra = %{evr}				\
Requires:	%{name}-tools = %{evr}					\
Requires(pre):	dracut							\
Requires(post): dracut							\
%{expand:%%description %{1}}						\
%{desc}									\
This subpackage provides support for %{1} systems.			\
									\
%{expand:%%{?!buildsubdir:%%define buildsubdir grub-%{1}-%{tarversion}}}\
%{expand:%%package %{1}-modules}					\
Summary:	Modules used to build custom grub images		\
Group:		System Environment/Base					\
BuildArch:	noarch							\
Requires:	%{name}-common = %{evr}					\
%{expand:%%description %{1}-modules}					\
%{desc}									\
This subpackage provides support for rebuilding your own grub.efi.	\
									\
%{expand:%%{?!buildsubdir:%%define buildsubdir grub-%{1}-%{tarversion}}}\
%{expand:%%package %{1}-tools}						\
Summary:	Support tools for GRUB.					\
Group:		System Environment/Base					\
Requires:	gettext os-prober which file system-logos		\
Requires:	%{name}-common = %{evr}					\
Requires:	%{name}-tools-minimal = %{evr}				\
Requires:	os-prober >= 1.58-11					\
Requires:	gettext which file					\
									\
%{expand:%%description %{1}-tools}					\
%{desc}									\
This subpackage provides tools for support of %{1} platforms.		\
%{nil}

%define define_efi_variant(p)						\
%{expand:%%package %{1}}						\
Summary:	GRUB for EFI systems.					\
Group:		System Environment/Base					\
Requires:	%{name}-common = %{evr}					\
Requires:	%{name}-tools-minimal >= %{evr}				\
Requires:	%{name}-tools-extra = %{evr}				\
Requires:	%{name}-tools = %{evr}					\
%{-p:Provides:	%{name}-efi = %{evr}}					\
%{-p:Obsoletes:	%{name}-efi < %{evr}}					\
									\
%{expand:%%description %{1}}						\
%{desc}									\
This subpackage provides support for %{1} systems.			\
									\
%{expand:%%{?!buildsubdir:%%define buildsubdir grub-%{1}-%{tarversion}}}\
%{expand:%%package %{1}-modules}					\
Summary:	Modules used to build custom grub.efi images		\
Group:		System Environment/Base					\
BuildArch:	noarch							\
Requires:	%{name}-common = %{evr}					\
%{expand:%%description %{1}-modules}					\
%{desc}									\
This subpackage provides support for rebuilding your own grub.efi.	\
									\
%{expand:%%package %{1}-cdboot}						\
Summary:	Files used to boot removeable media with EFI		\
Group:		System Environment/Base					\
Requires:	%{name}-common = %{evr}					\
%{expand:%%description %{1}-cdboot}					\
%{desc}									\
This subpackage provides optional components of grub used with removeable media on %{1} systems.\
%{nil}

%global do_common_setup()					\
%setup -D -q -T -a 0 -n grub-%{tarversion}			\
cd grub-%{tarversion}						\
rm -fv docs/*.info						\
cp %{SOURCE5} unifont.pcf.gz					\
cp %{SOURCE6} .gitignore					\
git init							\
echo '![[:digit:]][[:digit:]]_*.in' > util/grub.d/.gitignore	\
echo '!*.[[:digit:]]' > util/.gitignore				\
git config user.email "%{name}-owner@fedoraproject.org"		\
git config user.name "Fedora Ninjas"				\
git config gc.auto 0						\
git add .							\
git commit -a -q -m "%{tarversion} baseline."			\
git am %%{patches} </dev/null					\
git config --unset user.email					\
git config --unset user.name					\
cd ..								\
%{nil}

%global do_setup()						\
cp -al grub-%{tarversion} grub-%{1}-%{tarversion}		\
%{nil}

%global do_common_build()					\
cd %{common_srcdir}						\
makeinfo --info --no-split -I docs -o docs/grub-dev.info	\\\
	docs/grub-dev.texi					\
makeinfo --info --no-split -I docs -o docs/grub.info		\\\
	docs/grub.texi						\
makeinfo --html --no-split -I docs -o docs/grub-dev.html	\\\
	docs/grub-dev.texi					\
makeinfo --html --no-split -I docs -o docs/grub.html		\\\
	docs/grub.texi						\
cd ..								\
%{nil}

%define do_efi_configure()					\
./autogen.sh							\
%configure							\\\
	%{cc_equals}						\\\
	CFLAGS="%{2}"						\\\
	TARGET_LDFLAGS=-static					\\\
        --with-platform=efi					\\\
	--target=%{1}						\\\
	--with-grubdir=%{name}					\\\
        --program-transform-name=s,grub,%{name},		\\\
	--disable-grub-mount					\\\
	--disable-werror					\
%{nil}

%define do_efi_build_modules()					\
make %{?_smp_mflags} ascii.h widthspec.h			\
make %{?_smp_mflags} -C grub-core				\
%{nil}

%define do_efi_build_all()					\
make %{?_smp_mflags}						\
%{nil}

%define do_efi_link_utils()					\
for x in grub-mkimage ; do					\\\
	ln ../grub-%{1}-%{tarversion}/${x} ./ ;			\\\
done								\
%{nil}

%ifarch x86_64 aarch64
%define mkimage()						\
%{4}./grub-mkimage -O %{1} -o %{2}.orig				\\\
	-p /EFI/%{efidir} -d grub-core ${GRUB_MODULES}		\
%{4}./grub-mkimage -O %{1} -o %{3}.orig				\\\
	-p /EFI/BOOT -d grub-core ${GRUB_MODULES}		\
%{expand:%%{pesign -s -i %{2}.orig -o %{2} -a %{5} -c %{6} -n %{7}}}	\
%{expand:%%{pesign -s -i %{3}.orig -o %{3} -a %{5} -c %{6} -n %{7}}}	\
%{nil}
%else
%define mkimage()						\
%{4}./grub-mkimage -O %{1} -o %{2}				\\\
	-p /EFI/%{efidir} -d grub-core ${GRUB_MODULES}		\
%{4}./grub-mkimage -O %{1} -o %{3}				\\\
	-p /EFI/BOOT -d grub-core ${GRUB_MODULES}		\
%{nil}
%endif

%define do_efi_build_images()					\
GRUB_MODULES="	all_video boot btrfs cat chain configfile echo	\\\
		efifwsetup efinet ext2 fat font gfxmenu gfxterm \\\
		gzio halt hfsplus iso9660 jpeg loadenv loopback \\\
		lvm mdraid09 mdraid1x minicmd normal part_apple \\\
		part_msdos part_gpt password_pbkdf2 png reboot	\\\
		search search_fs_uuid search_fs_file		\\\
		search_label serial sleep syslinuxcfg test tftp \\\
		video xfs"					\
GRUB_MODULES+=%{efi_modules}					\
%{expand:%%{mkimage %{1} %{2} %{3} %{4} %{5} %{6} %{7}}}	\
%{nil}

%define do_primary_efi_build()					\
cd grub-%{1}-%{tarversion}					\
%{expand:%%do_efi_configure %%{4} %%{5}}			\
%do_efi_build_all						\
%{expand:%%do_efi_build_images %{grub_target_name} %{2} %{3} ./ %{6} %{7} %{8}}\
cd ..								\
%{nil}

%define do_alt_efi_build()					\
cd grub-%{1}-%{tarversion}					\
%{expand:%%do_efi_configure %%{4} %%{5}}			\
%do_efi_build_modules						\
%{expand:%%do_efi_link_utils %{grubefiarch}}			\
%{expand:%%do_efi_build_images %{alt_grub_target_name} %{2} %{3} ../grub-%{grubefiarch}-%{tarversion}/ %{6} %{7} %{8}}	\
cd ..								\
%{nil}

%define do_legacy_build()					\
cd grub-%{1}-%{tarversion}					\
./autogen.sh							\
%configure							\\\
	%{cc_equals}						\\\
	CFLAGS="%{legacy_cflags}"				\\\
	TARGET_LDFLAGS=-static					\\\
        --with-platform=%{platform}				\\\
	--target=%{_target_platform}				\\\
	--with-grubdir=%{name}					\\\
        --program-transform-name=s,grub,%{name},		\\\
	--disable-grub-mount					\\\
	--disable-werror					\
make %{?_smp_mflags}						\
cd ..								\
%{nil}

%define do_alt_efi_install()					\
cd grub-%{1}-%{tarversion}					\
install -d -m 755 $RPM_BUILD_ROOT/usr/lib/grub/%{grubaltefiarch}/ \
find . '(' -iname gdb_grub					\\\
	-o -iname kernel.exec					\\\
	-o -iname kernel.img					\\\
	-o -iname config.h					\\\
	-o -iname gmodule.pl					\\\
	-o -iname modinfo.sh					\\\
	-o -iname '*.lst'					\\\
	-o -iname '*.mod'					\\\
	')'							\\\
	-exec cp {} $RPM_BUILD_ROOT/usr/lib/grub/%{grubaltefiarch}/ \\\; \
find $RPM_BUILD_ROOT -iname "*.mod*" -exec chmod a-x {} '\;'	\
install -m 755 %{2} $RPM_BUILD_ROOT/boot/efi/EFI/%{efidir}/%{2}	\
install -m 755 %{3} $RPM_BUILD_ROOT/boot/efi/EFI/%{efidir}/%{3} \
cd ..								\
%{nil}

%define do_efi_install()					\
cd grub-%{1}-%{tarversion}					\
install -m 755 -d $RPM_BUILD_ROOT/boot/efi/EFI/%{efidir}/	\
install -m 755 -d $RPM_BUILD_ROOT/boot/grub2/			\
make DESTDIR=$RPM_BUILD_ROOT install				\
if [ -f $RPM_BUILD_ROOT%{_infodir}/grub.info ]; then		\
	rm -f $RPM_BUILD_ROOT%{_infodir}/grub.info		\
fi								\
if [ -f $RPM_BUILD_ROOT%{_infodir}/grub-dev.info ]; then	\
	rm -f $RPM_BUILD_ROOT%{_infodir}/grub-dev.info		\
fi								\
find $RPM_BUILD_ROOT -iname "*.module" -exec chmod a-x {} '\;'	\
touch $RPM_BUILD_ROOT/boot/efi/EFI/%{efidir}/grub.cfg		\
ln -sf ../boot/efi/EFI/%{efidir}/grub.cfg			\\\
	$RPM_BUILD_ROOT%{_sysconfdir}/%{name}-efi.cfg		\
install -m 755 %{2} $RPM_BUILD_ROOT/boot/efi/EFI/%{efidir}/%{2}	\
install -m 755 %{3} $RPM_BUILD_ROOT/boot/efi/EFI/%{efidir}/%{3} \
install -D -m 644 unicode.pf2					\\\
	$RPM_BUILD_ROOT/boot/efi/EFI/%{efidir}/fonts/unicode.pf2\
ln -sf /boot/efi/EFI/%{efidir}/grubenv				\\\
	$RPM_BUILD_ROOT/boot/grub2/grubenv			\
cd ..								\
%{nil}

%define do_legacy_install()					\
cd grub-%{1}-%{tarversion}					\
make DESTDIR=$RPM_BUILD_ROOT install				\
if [ -f $RPM_BUILD_ROOT%{_infodir}/grub.info ]; then		\
	rm -f $RPM_BUILD_ROOT%{_infodir}/grub.info		\
fi								\
if [ -f $RPM_BUILD_ROOT%{_infodir}/grub-dev.info ]; then	\
	rm -f $RPM_BUILD_ROOT%{_infodir}/grub-dev.info		\
fi								\
ln -s ../boot/%{name}/grub.cfg					\\\
	${RPM_BUILD_ROOT}%{_sysconfdir}/grub2.cfg		\
cd ..								\
if [ -f $RPM_BUILD_ROOT/%{_libdir}/grub/%{1}/grub2.chrp ]; then \
	mv $RPM_BUILD_ROOT/%{_libdir}/grub/%{1}/grub2.chrp	\\\
	   $RPM_BUILD_ROOT/%{_libdir}/grub/%{1}/grub.chrp	\
fi								\
%{nil}

%define do_common_install()					\
cd %{common_srcdir}						\
install -d -m 0755 						\\\
	$RPM_BUILD_ROOT%{_datarootdir}/locale/en\@quot		\\\
	$RPM_BUILD_ROOT%{_datarootdir}/locale/en		\\\
	$RPM_BUILD_ROOT%{_infodir}/				\
cp -a $RPM_BUILD_ROOT%{_datarootdir}/locale/en\@quot		\\\
	$RPM_BUILD_ROOT%{_datarootdir}/locale/en		\
cp docs/grub.info $RPM_BUILD_ROOT%{_infodir}/%{name}.info	\
cp docs/grub-dev.info						\\\
	$RPM_BUILD_ROOT%{_infodir}/%{name}-dev.info		\
# Ghost config file						\
install -d -m 0755 ${RPM_BUILD_ROOT}/boot/efi/EFI/%{efidir}/	\
install -d -m 0755 ${RPM_BUILD_ROOT}/boot/grub2/		\
install -d -m 0755 ${RPM_BUILD_ROOT}/boot/%{name}/themes/system	\
install -d -m 0755 ${RPM_BUILD_ROOT}%{_sysconfdir}/default	\
install -d -m 0755 ${RPM_BUILD_ROOT}%{_sysconfdir}/sysconfig	\
touch ${RPM_BUILD_ROOT}%{_sysconfdir}/default/grub		\
ln -sf %{_sysconfdir}/default/grub				\\\
	${RPM_BUILD_ROOT}%{_sysconfdir}/sysconfig/grub		\
touch ${RPM_BUILD_ROOT}/boot/%{name}/grub.cfg			\
./grub-editenv ${RPM_BUILD_ROOT}/boot/grub2/grubenv create	\
ln -sf /boot/efi/EFI/%{efidir}/grubenv				\\\
	${RPM_BUILD_ROOT}/boot/grub2/grubenv			\
cd ..								\
%{nil}

%define define_legacy_variant_files()				\
%{expand:%%files %{1}}						\
%defattr(-,root,root,-)						\
%config(noreplace) %{_sysconfdir}/%{name}.cfg			\
%ghost %config(noreplace) /boot/%{name}/grub.cfg		\
								\
%{expand:%%files %{1}-modules}					\
%defattr(-,root,root)						\
%dir %{_libdir}/grub/%{2}/					\
%{_libdir}/grub/%{2}/*						\
%exclude %{_libdir}/grub/%{2}/*.module				\
%exclude %{_libdir}/grub/%{2}/{boot,boot_hybrid,cdboot,diskboot,lzma_decompress,pxeboot}.{image,img}						\
%exclude %{_libdir}/grub/%{2}/*.o				\
%{nil}

%define define_efi_variant_files()				\
%{expand:%%files %{1}}						\
%defattr(-,root,root,-)						\
%config(noreplace) %{_sysconfdir}/%{name}-efi.cfg		\
%dir %attr(0755,root,root)/boot/efi/EFI/%{efidir}		\
/boot/efi/EFI/%{efidir}/%{2}					\
%attr(0755,root,root)/boot/efi/EFI/%{efidir}/fonts		\
%ghost %config(noreplace) /boot/efi/EFI/%{efidir}/grub.cfg	\
/boot/grub2/grubenv						\
%ghost %config(noreplace) %attr(0700,root,root)/boot/efi/EFI/%{efidir}/grubenv \
								\
%{expand:%%files %{1}-modules}					\
%defattr(-,root,root,-)						\
%dir %{_libdir}/grub/%{6}/					\
%{_libdir}/grub/%{6}/*						\
%exclude %{_libdir}/grub/%{6}/*.module				\
								\
%{expand:%%files %{1}-cdboot}					\
%defattr(-,root,root,-)						\
%attr(0755,root,root)/boot/efi/EFI/%{efidir}/%{3}		\
%attr(0755,root,root)/boot/efi/EFI/%{efidir}/fonts		\
%{nil}
