## START: Set by rpmautospec
## (rpmautospec version 0.6.0)
## RPMAUTOSPEC: autorelease, autochangelog
%define autorelease(e:s:pb:n) %{?-p:0.}%{lua:
    release_number = 1;
    base_release_number = tonumber(rpm.expand("%{?-b*}%{!?-b:1}"));
    print(release_number + base_release_number - 1);
}%{?-e:.%{-e*}}%{?-s:.%{-s*}}%{!?-n:%{?dist}}
## END: Set by rpmautospec

# simfail runs on bare metal but not on Koji
%bcond_with check

%global toolchain clang

# upstream build system requires the use of libbpf and bpftool sources
# see https://github.com/anakryiko/retsnoop/issues/22
%global bpftool_url https://github.com/libbpf/bpftool
%global bpftool_commit 64402b8ea43b2ef62ee8ba71d9b50a5e32603fd2
%global bpftool_shortcommit %(c=%{bpftool_commit}; echo ${c:0:7})
%global bpftool_date 20240716
# see bpftool/src/main.c
%global bpftool_version 7.5.0^%{bpftool_date}git%{bpftool_shortcommit}

%global libbpf_url https://github.com/libbpf/libbpf
%global libbpf_commit 686f600bca59e107af4040d0838ca2b02c14ff50
%global libbpf_shortcommit %(c=%{libbpf_commit}; echo ${c:0:7})
%global libbpf_date 20240710
# <mock-chroot> sh-5.2# ./bpftool/bootstrap/bpftool version
# bpftool v7.5.0
# using libbpf v1.5
%global libbpf_version 1.5^%{libbpf_date}git%{libbpf_shortcommit}

Name:           retsnoop
Version:        0.10.1
Release:        %autorelease
Summary:        A tool for investigating kernel error call stacks

# retsnoop: BSD-2-Clause
# bundled libbpf: LGPL-2.1-only OR BSD-2-Clause
# statically linked rust crates
# taken from %%cargo_license_summary
# 0BSD OR MIT OR Apache-2.0
# Apache-2.0 OR MIT
# MIT
# MIT OR Apache-2.0
# MIT OR Zlib OR Apache-2.0
# Unlicense OR MIT
License:        BSD-2-Clause AND (LGPL-2.1-only OR BSD-2-Clause) AND (Apache-2.0 OR MIT) AND MIT AND (0BSD OR Apache-2.0 OR MIT) AND (MIT OR Unlicense) AND (Apache-2.0 OR MIT OR Zlib)
URL:            https://github.com/anakryiko/retsnoop
Source0:        %{url}/archive/v%{version}/%{name}-%{version}.tar.gz
Source1:        %{bpftool_url}/archive/%{bpftool_commit}/bpftool-%{bpftool_version}.tar.gz
Source2:        %{libbpf_url}/archive/%{libbpf_commit}/libbpf-%{libbpf_version}.tar.gz
Source3:        README.Fedora

# has a Rust component
ExclusiveArch:  %{rust_arches}
# rust syn crate not compiling on 32-bit ARM
ExcludeArch:    armv7hl
# bpftool not compiling on ix86
# we don't ship 32-bit binaries anyway, but it'd be good to test compilation
# once this is fixed https://github.com/libbpf/bpftool/issues/158
ExcludeArch:    %{ix86}

BuildRequires:  cargo-rpm-macros >= 24
BuildRequires:  clang
BuildRequires:  llvm
BuildRequires:  make
# libbpf deps
BuildRequires:  elfutils-libelf-devel

# upstream does not support dynamic linking
Provides:       bundled(bpftool) = %{bpftool_version}
Provides:       bundled(libbpf) = %{libbpf_version}

%description
retsnoop is BPF-based tool that is meant to help debugging kernel issues. It
allows to capture call stacks of kernel functions that return errors (NULL or
-Exxx) and emits every such detected call stack, along with the captured
results.


%prep
%autosetup -p1
# provide the bpftool version we specify rather than using git submodule
tar xf %{SOURCE1}
rmdir bpftool
mv bpftool-%{bpftool_commit} bpftool
# same license files as libbpf, no-op

# provide the libbpf version we specify rather than using git submodule
tar xf %{SOURCE2}
rmdir libbpf
mv libbpf-%{libbpf_commit} libbpf
mv libbpf/LICENSE libbpf-LICENSE
mv libbpf/LICENSE.BSD-2-Clause  libbpf-LICENSE.BSD-2-Clause
mv libbpf/LICENSE.LGPL-2.1 libbpf-LICENSE.LGPL-2.1

# README.Fedora
cp -p %{SOURCE3} .

# Rust parts
# the path is hardcoded, make sure Cargo.toml is found in the expected place
ln -s sidecar/Cargo.toml .
# the checksums in the lock file might not match
rm sidecar/Cargo.lock
%cargo_prep

%generate_buildrequires
%cargo_generate_buildrequires


%build
%if 0%{?fedora} < 36
# this is only called automatically after
# https://www.fedoraproject.org/wiki/Changes/SetBuildFlagsBuildCheck
%set_build_flags
%endif
cd sidecar
%cargo_build
%{cargo_license_summary}
%{cargo_license} > ../LICENSE.dependencies
cd -
%make_build -C src HOSTCC=clang


%install
%make_install -C src prefix=%{_prefix}


%if %{with check}
%check
./src/simfail -a
%endif


%files
%license LICENSE libbpf-LICENSE*
%license LICENSE.dependencies
%doc README.md README.Fedora
%{_bindir}/retsnoop
%{_bindir}/simfail


%changelog
## START: Generated by rpmautospec
* Mon Oct 14 2024 Davide Cavalca <dcavalca@fedoraproject.org> - 0.10.1-1
- Update to 0.10.1; Fixes: RHBZ#2317860

* Tue Sep 03 2024 Michel Lind <salimma@fedoraproject.org> - 0.10-2
- Temporarily disable ix86 build

* Tue Sep 03 2024 Michel Lind <salimma@fedoraproject.org> - 0.10-1
- Update to version 0.10

* Thu Jul 25 2024 Michel Lind <salimma@fedoraproject.org> - 0.9.8-3
- Bump addrline from 0.21 to 0.24
- Bump gimli from 0.28 to 0.31
- Bump object from 0.32 to 0.36

* Fri Jul 19 2024 Fedora Release Engineering <releng@fedoraproject.org> - 0.9.8-2
- Rebuilt for https://fedoraproject.org/wiki/Fedora_41_Mass_Rebuild

* Thu Jan 25 2024 Michel Lind <salimma@fedoraproject.org> - 0.9.8-1
- Update to 0.9.8

* Mon Jan 22 2024 Fedora Release Engineering <releng@fedoraproject.org> - 0.9.7-3
- Rebuilt for https://fedoraproject.org/wiki/Fedora_40_Mass_Rebuild

* Mon Oct 02 2023 Michel Lind <salimma@fedoraproject.org> - 0.9.7-2
- Fix 32-bit builds

* Mon Oct 02 2023 Michel Lind <salimma@fedoraproject.org> - 0.9.7-1
- Update to 0.9.7

* Fri Jul 21 2023 Fedora Release Engineering <releng@fedoraproject.org> - 0.9.6-2
- Rebuilt for https://fedoraproject.org/wiki/Fedora_39_Mass_Rebuild

* Fri Mar 31 2023 Davide Cavalca <dcavalca@fedoraproject.org> - 0.9.6-1
- Update to 0.9.6; Fixes: RHBZ#2181684

* Thu Mar 30 2023 Fabio Valentini <decathorpe@gmail.com> - 0.9.5-2
- Bump addr2line to 0.19, gimli to 0.27, and object to 0.30

* Thu Feb 16 2023 Michel Alexandre Salim <salimma@fedoraproject.org> - 0.9.5-1
- Update to 0.9.5

* Tue Feb 07 2023 Michel Alexandre Salim <salimma@fedoraproject.org> - 0.9.4-1
- Update to 0.9.4

* Sun Feb 05 2023 Fabio Valentini <decathorpe@gmail.com> - 0.9.3-5
- Rebuild for fixed frame pointer compiler flags in Rust RPM macros

* Fri Jan 20 2023 Fedora Release Engineering <releng@fedoraproject.org> - 0.9.3-4
- Rebuilt for https://fedoraproject.org/wiki/Fedora_38_Mass_Rebuild

* Wed Nov 09 2022 Michel Alexandre Salim <salimma@fedoraproject.org> - 0.9.3-3
- Explicitly require llvm

* Tue Nov 08 2022 Michel Alexandre Salim <salimma@fedoraproject.org> - 0.9.3-2
- Update sources

* Tue Nov 08 2022 Michel Alexandre Salim <salimma@fedoraproject.org> - 0.9.3-1
- Update to 0.9.3; this adds KASLR support Document installing `kernel-
  debuginfo` for better tracing

* Wed Oct 26 2022 Michel Alexandre Salim <salimma@fedoraproject.org> - 0.9.2-2
- Actually copy in README.Fedora so it can be used

* Wed Oct 26 2022 Michel Alexandre Salim <salimma@fedoraproject.org> - 0.9.2-1
- Initial Fedora release
## END: Generated by rpmautospec