%global pkgvers 0
%global scdate0 20241216
%global schash0 8975139745e424294846c0e34051d91fab242974
%global branch0 main
%global source0 https://github.com/picolibc/picolibc.git

%global sshort0 %{expand:%%{lua:print(('%{schash0}'):sub(1,8))}}

%define sysarch unknown-elf
%define targets riscv64;xtensa

Name:           cross-%{sysarch}-picolibc
Version:        1.8.8
Release:        %{scdate0}.%{pkgvers}.git%{sshort0}%{?dist}
Summary:        C library designed for embedded
License:        BSD and MIT and LGPLv2+ and ISC
BuildArch:      noarch

URL:            https://keithp.com/picolibc

BuildRequires:  git gcc-c++ meson ninja-build

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

%{lua:
for target in string.gmatch(rpm.expand("%{cpulist}"), "[%w_-]+")
do
  print("BuildRequires:  "..target.."-"..rpm.expand("%{sysarch}").."-gcc\n")
  print("BuildRequires:  "..target.."-"..rpm.expand("%{sysarch}").."-binutils\n")
end}

%undefine _auto_set_build_flags
%global __os_install_post /usr/lib/rpm/brp-compress
%global _binaries_in_noarch_packages_terminate_build 0
%global __provides_exclude_from ^(%{_prefix}/.*-%{sysarch}/.*$)
%global __requires_exclude_from ^(%{_prefix}/.*-%{sysarch}/.*$)

%description
Picolibc is a C library intended for use on embedded systems.

%{lua:
for target in string.gmatch(rpm.expand("%{cpulist}"), "[%w_-]+")
do
  print("%package -n "..target.."-"..rpm.expand("%{sysarch}").."-picolibc\n")
  print("Summary: C library intended for use on "..target.." embedded systems\n")
  print("Provides:  "..target.."-picolibc\n")
  print("Provides:  "..target.."-libc\n")
  print("%description -n "..target.."-"..rpm.expand("%{sysarch}").."-picolibc\n")
  print("Picolibc is a C library intended for use on "..target.." embedded systems.\n")
  print("\n")
end}


%prep
%setup -T -c -n %{name}
git clone --depth 1 -n -b %{branch0} %{source0} .
git fetch --depth 1 origin %{schash0}
git reset --hard %{schash0}
git log --format=fuller


%build
for target in $(echo "%{cpulist}" | sed 's|;| |g');
do
  endian="little"
  case $target in
    riscv64*)
      endian="little"
    ;;
  esac
  echo "Building for [$target]"
  mkdir -p build-$target-%{sysarch}
  pushd build-$target-%{sysarch}
  # generate arch file
  ../scripts/GeneratePicolibcCrossFile.sh --endianness=$endian \
  --target-arch=$target-%{sysarch} > cross-$target-%{sysarch}.txt
  # built for target arch
  meson .. \
    -Dprefix=/ \
    -Dlibdir=%{_prefix}/$target-%{sysarch}/lib \
    -Dincludedir=%{_prefix}/$target-%{sysarch}/include \
    --cross-file cross-$target-%{sysarch}.txt
  ninja %{?_smp_mflags}
  popd
done


%install
for target in $(echo "%{cpulist}" | sed 's|;| |g');
do
  echo "Installing for [$target-%{sysarch}]"
  pushd build-$target-%{sysarch}
    DESTDIR=%{buildroot} ninja install
    # we are a cross version
    rm -rf %{buildroot}%{_infodir}
  popd
done


%{lua:
for target in string.gmatch(rpm.expand("%{cpulist}"), "[%w_-]+")
do
  print("%files -n "..target.."-"..rpm.expand("%{sysarch}").."-picolibc\n")
  print("%doc README.md\n")
  print("%license COPYING*\n")
  print("%dir "..rpm.expand("%{_prefix}/")..target.."-"..rpm.expand("%{sysarch}").."\n")
  print("%dir "..rpm.expand("%{_prefix}/")..target.."-"..rpm.expand("%{sysarch}").."/include\n")
  print(rpm.expand("%{_prefix}/")..target.."-"..rpm.expand("%{sysarch}").."/include/*\n")
  print("%dir "..rpm.expand("%{_prefix}/")..target.."-"..rpm.expand("%{sysarch}").."/lib\n")
  print(rpm.expand("%{_prefix}/")..target.."-"..rpm.expand("%{sysarch}").."/lib/*\n")
  print(rpm.expand("%{_prefix}/lib/gcc/")..target.."-"..rpm.expand("%{sysarch}").."/*/*.specs\n")
  print("\n")
end}


%changelog
* Fri May 27 2022 Cristian Balint <cristian.balint@gmail.com>
- github update releases