%global gem_name ffi
Name:                rubygem-%{gem_name}
Version:             1.10.0
Release:             5
Summary:             FFI Extensions for Ruby
License:             BSD-3-Clause and MIT
URL:                 https://www.github.com/ffi/ffi
Source0:             https://rubygems.org/gems/%{gem_name}-%{version}.gem
Source1:             https://www.github.com/ffi/%{gem_name}/archive/%{version}.tar.gz
Source2:             ffi-1.10.0-loongarch64.gemspec
Patch0:              Remove-taint-support.patch
Patch1:              0001-add-loongarch64-support-for-ffi.patch
Patch2:              0002-Add-types.conf-for-riscv64-linux.patch
BuildRequires:       ruby(release) rubygems-devel ruby-devel gcc libffi-devel rubygem(rspec)
%description
Ruby-FFI is a ruby extension for programmatically loading dynamic
libraries, binding functions within them, and calling those functions
from Ruby code. Moreover, a Ruby-FFI extension works without changes
on Ruby and JRuby. Discover why should you write your next extension
using Ruby-FFI here[http://wiki.github.com/ffi/ffi/why-use-ffi].

%package doc
Summary:             Documentation for %{name}
Requires:            %{name} = %{version}-%{release}
BuildArch:           noarch
%description doc
Documentation for %{name}.

%prep
%setup -q -n  %{gem_name}-%{version} -b 1
ln -s %{gem_name}-%{version}/test test
ln -s %{gem_name}-%{version}/spec spec
%patch0 -p1
%ifarch loongarch64
%patch1 -p1
%endif
%patch2 -p1

%build
%ifarch loongarch64
gem build %{SOURCE2}
%else
gem build ../%{gem_name}-%{version}.gemspec
%endif
%gem_install

%install
mkdir -p %{buildroot}%{gem_dir}
%ifarch riscv64
cp -r lib/ffi/platform/riscv64-linux/ .%{gem_instdir}/lib/ffi/platform/
%endif
cp -a .%{gem_dir}/* \
        %{buildroot}%{gem_dir}/
mkdir -p %{buildroot}%{gem_extdir_mri}
cp -a .%{gem_extdir_mri}/{gem.build_complete,*.so} %{buildroot}%{gem_extdir_mri}/
rm -rf %{buildroot}%{gem_instdir}/ext/

%check
pushd .%{gem_instdir}
ln -s %{_builddir}/%{gem_name}-%{version}/spec spec
pushd spec/ffi/fixtures
make JFLAGS="%{optflags}"
popd
rspec -I$(dirs +1)%{gem_extdir_mri} spec
popd

%files
%dir %{gem_instdir}
%{gem_extdir_mri}
%exclude %{gem_instdir}/.*
%license %{gem_instdir}/COPYING
%license %{gem_instdir}/LICENSE
%license %{gem_instdir}/LICENSE.SPECS
%exclude %{gem_instdir}/appveyor.yml
%{gem_libdir}
%exclude %{gem_cache}
%{gem_spec}

%files doc
%doc %{gem_docdir}
%doc %{gem_instdir}/CHANGELOG.md
%{gem_instdir}/Gemfile
%doc %{gem_instdir}/README.md
%{gem_instdir}/Rakefile
%{gem_instdir}/samples
%{gem_instdir}/ffi.gemspec

%changelog
* Mon Jul 10 2023 laokz <zhangkai@iscas.ac.cn> - 1.10.0-5
- backport upstream patch to support riscv64

* Thu May 18 2023 Wenlong Zhang <zhangwenlong@loongson.cn> - 1.10.0-4
- add loongarch64 support for rubygem-ffi

* Tue May 10 2022 yaoxin <yaoxin30@h-partners.com> - 1.10.0-3
- License compliance rectification

* Wed Jan 26 2022 liyanan <liyanan32@huawei.com> - 1.10.0-2
- Remove taint support

* Thu Aug 20 2020 xiezheng <xiezheng4@huawei.com> - 1.10.0-1
- package init
