%global pkgvers 0
%global scdate0 20241022
%global schash0 61399d4bd02ae1ec03068445aa7ffe9174466bfd
%global branch0 master
%global source0 https://github.com/bminor/musl.git

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

%define sysarch linux-gnu
%define targets aarch64;ppc64le

Name:           cross-%{sysarch}-musl
Version:        1.2.5
Release:        %{scdate0}.%{pkgvers}.git%{sshort0}%{?dist}
Summary:        C library designed for embedded
License:        MIT
BuildArch:      noarch

URL:            https://musl.libc.org

BuildRequires:  git gcc-c++

%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
Musl 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}").."-musl\n")
  print("Summary: C library intended for use on "..target.." embedded systems\n")
  print("Provides:  "..target.."-musl\n")
  print("Provides:  "..target.."-libc\n")
  print("%description -n "..target.."-"..rpm.expand("%{sysarch}").."-musl\n")
  print("Musl 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
sed -i '/fail "$0: error: unsupported long double type"/d' configure
sed -i '/compiler_defines_long_double_incorrectly/d' src/stdio/vfprintf.c
for target in $(echo "%{cpulist}" | sed 's|;| |g');
do
  echo "Building for [$target]"
  mkdir -p build-$target-%{sysarch}
  pushd build-$target-%{sysarch}
  ../configure \
      --prefix=%{_prefix}/$target-%{sysarch} \
      --libdir=%{_prefix}/$target-%{sysarch}/lib \
      --disable-shared \
      --target=$target-%{sysarch}
  make %{?_smp_mflags}
  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 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}").."-musl\n")
  print("%doc README\n")
  print("%license COPYRIGHT\n")
  print("%dir "..rpm.expand("%{_prefix}").."/"..target.."-"..rpm.expand("%{sysarch}").."/bin\n")
  print(rpm.expand("%{_prefix}").."/"..target.."-"..rpm.expand("%{sysarch}").."/bin/*\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("\n")
end}


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