%global pkgvers 0
%global scdate0 20250202
%global sctags0 binutils-2_44
%global scfilt0 tags/binutils.*^{}
%global source0 https://github.com/bminor/binutils-gdb.git

%define sysarch linux-gnu
%define targets aarch64;or1k;ppc64le;riscv32;riscv64

Name:           cross-%{sysarch}-binutils
Version:        %(curl -s https://raw.githubusercontent.com/bminor/binutils-gdb/%{sctags0}/bfd/version.m4 | cut -d"[" -f3 | cut -d"]" -f1)
Release:        %{scdate0}.%{pkgvers}%{?dist}
Summary:        GNU Binutils for cross-compilation targets
License:        GPLv2+ and GPLv3+ and LGPLv2+ and BSD

URL:            https://www.sourceware.org/binutils

BuildRequires:  gcc flex bison git texinfo texinfo-tex
BuildRequires:  perl-podlators zlib-devel readline-devel
%if 0%{?fedora}
BuildRequires:  ppl-devel cloog
%endif

%global _default_patch_fuzz 500
%define cpulist %(echo "%{targets}" | sed "s|%{_arch}||g")

%description
This is a cross-compilation version of GNU Binutils, which can be used to
assemble and link binaries for the target platform.

%{lua:
for target in string.gmatch(rpm.expand("%{cpulist}"), "[%w_-]+")
do
  print("%package -n "..target.."-"..rpm.expand("%{sysarch}").."-binutils\n")
  print("Summary: GNU Binutils for cross-compilation to "..target.." target\n")
  print("Provides: "..target.."-binutils = "..rpm.expand("%{version}").."\n")
  print("%description -n "..target.."-"..rpm.expand("%{sysarch}").."-binutils\n")
  print("GNU Binutils for cross-compilation to "..target.." target\n")
  print("\n")
end}


%prep
%setup -T -c -n %{name}
git clone --depth 1 -n -b %{sctags0} %{source0} .
git reset --hard %{sctags0}
git --no-pager log --format=fuller
%autosetup -D -p 1 -T -c -n %{name}
rm -rf gdb sim gprofng


%build
for target in $(echo "%{cpulist}" | sed 's|;| |g');
do
  cargs=''
  case $target in
    i?86*|sparc*|ppc*|s390*|sh*|arm*|aarch64*|riscv*)
      cargs="$cargs --enable-64-bit-bfd"
    ;;
  esac
  echo "Building for [$target]"
  mkdir -p build-$target-%{sysarch}/gas/doc
  pushd build-$target-%{sysarch}
  ../configure CFLAGS="%{optflags} -Wno-error=format-security" \
    --prefix=%{_prefix} --libdir=%{_libdir} \
    --with-docdir=share/doc/%{name} \
    --mandir=%{_mandir} --infodir=%{_infodir} \
    --with-pkgversion="RedHat %{version}-%{release}" \
    --target=$target-%{sysarch} \
    --with-gcc --with-gnu-as \
    --with-gnu-ld --disable-gold \
    --with-debuginfod=no \
    --with-system-zlib \
    --enable-lto \
    --enable-plugins \
    --enable-multilib \
    --enable-interwork \
    --disable-nls \
    --disable-werror \
    --disable-shared \
    --disable-threads \
    $cargs
  %make_build
  popd
done


%install
for target in $(echo "%{cpulist}" | sed 's|;| |g');
do
  echo "Installing for [$target-%{sysarch}]"
  pushd build-$target-%{sysarch}
    make install DESTDIR=%{buildroot}
    # we don't want these as we are a cross version
    rm -rf %{buildroot}/%{_infodir}
    rm -rf %{buildroot}/%{_libdir}/bfd-plugins
  popd
done


%{lua:
for target in string.gmatch(rpm.expand("%{cpulist}"), "[%w_-]+")
do
  print("%files -n "..target.."-"..rpm.expand("%{sysarch}").."-binutils\n")
  print("%doc ChangeLog\n")
  print("%license COPYING*\n")
  print(rpm.expand("%{_bindir}").."/"..target.."-"..rpm.expand("%{sysarch}").."-*\n")
  print(rpm.expand("%{_prefix}").."/"..target.."-"..rpm.expand("%{sysarch}").."\n")
  print(rpm.expand("%{_mandir}").."/man1/"..target.."-*.1.gz\n")
  print("\n")
end}


%changelog
* Fri Aug 07 2020 Cristian Balint <cristian.balint@gmail.com>
- git upstream releases