%bcond_with check

# Only arches that are supported upstream as multilib and that the distro
# has multilib builds for should set build_multilib 1. In practice that
# is only x86_64 and ppc64 (but not in fedora 21 and later, and never
# for ppc64le).
%global build_multilib 0

%ifarch x86_64 ppc64
%global build_multilib 1
%endif

# Vine Linux doesn't have openmpi package
#ifarch %{ix86} x86_64 ppc ppc64 ppc64le %{arm} aarch64
#global build_openmpi 1
#else
%global build_openmpi 0
#endif

%global run_full_regtest 0
%global build_tools_devel 0

# Don't run dwz or generate minisymtab, valgrind doesn't handle compressed
# DWARF very well and it might read its own vgpreload libraries. Generating
# minisymtabs doesn't really work for the staticly linked tools.
%define _find_debuginfo_dwz_opts %{nil}
%undefine _include_minidebuginfo

Summary:        Tool for finding memory management bugs in programs
Summary(ja):    プログラムのメモリ管理バグを発見するためのツール
Name:           valgrind
Version:        3.23.0
Release:        1%{?_dist_release}
Group:          programming
Vendor:         Project Vine
Distribution:   Vine Linux

License:        GPLv2+
URL:            https://valgrind.org/
Source0:        https://sourceware.org/pub/valgrind/%{name}-%{version}.tar.bz2

# Needs investigation and pushing upstream
Patch1: valgrind-3.9.0-cachegrind-improvements.patch

# Make ld.so supressions slightly less specific.
Patch2: valgrind-3.9.0-ldso-supp.patch

# Add some stack-protector
Patch3: valgrind-3.16.0-some-stack-protector.patch

# Add some -Wl,z,now.
Patch4: valgrind-3.16.0-some-Wl-z-now.patch

# Patches from upstream VALGRIND_3_23_BRANCH
Patch5: 0001-Prepare-NEWS-for-branch-3.23-fixes.patch
Patch6: 0002-486180-MIPS-VexGuestArchState-has-no-member-named-gu.patch
Patch7: 0003-Bug-486293-memccpy-false-positives.patch
Patch8: 0004-Bug-486569-linux-inotify_init-syscall-wrapper-missin.patch
Patch9: 0005-aarch64-frinta-and-frinta-vector-instructions.patch
Patch10: 0006-mips-skip-using-shared-syscall-numbers-for-mips32.patch
Patch11: 0007-Fix-uninitialized-err-in-handle_extension.patch
Patch12: 0008-Avoid-use-of-guest_IP_AT_SYSCALL-in-handle_extension.patch
Patch13: 0009-s390x-Minor-fixes-in-extension-s390x.c.patch
Patch14: 0010-Bug-453044-gbserver_tests-failures-in-aarch64.patch
Patch15: 0011-Linux-regtest-reallocarray-needs-malloc.h.patch

BuildRoot: 	%{_tmppath}/%{name}-%{version}-root
%if %{build_multilib}
# Ensure glibc{,-devel} is installed for both multilib arches
BuildRequires: 	compat32-glibc-devel
%endif
BuildRequires: 	glibc-devel >= 2.14
%if %{build_openmpi}
BuildRequires: openmpi-devel >= 1.3.3
%endif

# For %%build and %%check.
# In case of a software collection, pick the matching gdb and binutils.
BuildRequires: 	gdb
BuildRequires: 	binutils

# gdbserver_tests/filter_make_empty uses ps in test
BuildRequires: 	procps

ExclusiveArch: %{ix86} x86_64 ppc ppc64 ppc64le s390x armv7hl aarch64
%ifarch %{ix86}
%define valarch x86
%define valsecarch %{nil}
%endif
%ifarch x86_64
%define valarch amd64
%define valsecarch x86
%endif
%ifarch ppc
%define valarch ppc32
%define valsecarch %{nil}
%endif
%ifarch ppc64
  %define valarch ppc64be
  %if %{build_multilib}
    %define valsecarch ppc32
  %else
    %define valsecarch %{nil}
  %endif
%endif
%ifarch ppc64le
%define valarch ppc64le
%define valsecarch %{nil}
%endif
%ifarch s390x
%define valarch s390x
%define valsecarch %{nil}
%endif
%ifarch armv7hl
%define valarch arm
%define valsecarch %{nil}
%endif
%ifarch aarch64
%define valarch arm64
%define valsecarch %{nil}
%endif

# Don't run dwz or generate minisymtab, valgrind doesn't handle compressed
# DWARF very well and it might read its own vgpreload libraries. Generating
# minisymtabs doesn't really work for the staticly linked tools.
%define _find_debuginfo_dwz_opts %{nil}
%undefine _include_minidebuginfo

%description 
Valgrind is a tool to help you find memory-management problems in your
programs. When a program is run under Valgrind's supervision, all
reads and writes of memory are checked, and calls to
malloc/new/free/delete are intercepted. As a result, Valgrind can
detect a lot of problems that are otherwise very hard to
find/diagnose.


%package devel
Summary:	Development files for valgrind
Summary(ja):	Valgrind の開発用ファイル
Group:		programming
Requires: 	%{name} = %{version}-%{release}
Provides: 	%{name}-static = %{version}-%{release}

%description devel
Header files and libraries for development of valgrind aware programs
or valgrind plugins.


%package openmpi
Summary: 	OpenMPI support for valgrind
Summary(ja): 	Valgrind の OpenMPI サポート
Group:		programming
Requires: 	%{name} = %{version}-%{release}

%description openmpi
A wrapper library for debugging OpenMPI parallel programs with valgrind.
See the section on Debugging MPI Parallel Programs with Valgrind in the
Valgrind User Manual for details.


%debug_package


%prep
%setup -q -n %{name}-%{version}
%autopatch -p1

# Some patches (might) touch Makefile.am or configure.ac files.
# Just always autoreconf so we don't need patches to prebuild files.
./autogen.sh


%build
# LTO triggers undefined symbols in valgrind.  Valgrind has a --enable-lto
# configure time option, but that doesn't seem to help.
# Disable LTO for now.
%define _lto_cflags %{nil}

CC=gcc
%if %{build_multilib}
# Ugly hack - libgcc 32-bit package might not be installed
mkdir -p shared/libgcc/32
ar r shared/libgcc/32/libgcc_s.a
ar r shared/libgcc/libgcc_s_32.a
CC="gcc -B `pwd`/shared/libgcc/"
%endif

# Old openmpi-devel has version depended paths for mpicc.
%if %{build_openmpi}
%if 0%{?fedora} >= 13 || 0%{?rhel} >= 6
%define mpiccpath %{!?scl:%{_libdir}}%{?scl:%{_root_libdir}}/openmpi/bin/mpicc
%else
%define mpiccpath %{!?scl:%{_libdir}}%{?scl:%{_root_libdir}}/openmpi/*/bin/mpicc
%endif
%else
# We explicitly don't want the libmpi wrapper. So make sure that configure
# doesn't pick some random mpi compiler that happens to be installed.
%define mpiccpath /bin/false
%endif

# Filter out some flags that cause lots of valgrind test failures.
# Also filter away -O2, valgrind adds it wherever suitable, but
# not for tests which should be -O0, as they aren't meant to be
# compiled with -O2 unless explicitely requested. Same for any -mcpu flag.
# Ideally we will change this to only be done for the non-primary build
# and the test suite. Also disable strict symbol checks because the
# vg_preload library will use hidden/undefined symbols from glibc
# like __libc_freeres.
%undefine _hardened_build
%undefine _strict_symbol_defs_build
CFLAGS="`echo " %{optflags} " | sed 's/ -fstack-protector\([-a-z]*\) / / g;s/ -O2 / /g;'`"
LDFLAGS="`echo " %{build_ldflags} "    | sed 's/ -Wl,-z,now / / g;'`"

export CFLAGS
export LDFLAGS

%configure CC="$CC" CFLAGS="$OPTFLAGS" CXXFLAGS="$OPTFLAGS" \
  --with-mpicc=%{mpiccpath} \
  GDB=%{_bindir}/gdb

make %{?_smp_mflags}


%install
rm -rf $RPM_BUILD_ROOT
make DESTDIR=$RPM_BUILD_ROOT install
rm -rf docs/installed
mkdir docs/installed
mv $RPM_BUILD_ROOT%{_datadir}/doc/valgrind/* docs/installed/
rm -f docs/installed/*.ps

# We want the MPI wrapper installed under the openmpi libdir so the script
# generating the MPI library requires picks them up and sets up the right
# openmpi libmpi.so requires. Install symlinks in the original/upstream
# location for backwards compatibility.
%if %{build_openmpi}
pushd $RPM_BUILD_ROOT%{_libdir}
mkdir -p openmpi/valgrind
cd valgrind
mv libmpiwrap-%{valarch}-linux.so ../openmpi/valgrind/
ln -s ../openmpi/valgrind/libmpiwrap-%{valarch}-linux.so
popd
%endif

%if "%{valsecarch}" != ""
pushd $RPM_BUILD_ROOT%{_libdir}/valgrind/
rm -f *-%{valsecarch}-* || :
for i in *-%{valarch}-*; do
  j=`echo $i | sed 's/-%{valarch}-/-%{valsecarch}-/'`
  ln -sf ../../lib/valgrind/$j $j
done
popd
%endif

%if %{build_tools_devel}
%ifarch %{ix86} x86_64
# To avoid multilib clashes in between i?86 and x86_64,
# tweak installed <valgrind/config.h> a little bit.
for i in HAVE_PTHREAD_CREATE_GLIBC_2_0 HAVE_PTRACE_GETREGS HAVE_AS_AMD64_FXSAVE64 \
         ; do
  sed -i -e 's,^\(#define '$i' 1\|/\* #undef '$i' \*/\)$,#ifdef __x86_64__\n# define '$i' 1\n#endif,' \
    $RPM_BUILD_ROOT%{_includedir}/valgrind/config.h
done
%endif
%else
# Remove files we aren't going to package.
# See tools-devel files.
rm $RPM_BUILD_ROOT%{_includedir}/valgrind/config.h
rm $RPM_BUILD_ROOT%{_includedir}/valgrind/libvex*h
rm $RPM_BUILD_ROOT%{_includedir}/valgrind/pub_tool_*h
rm -rf $RPM_BUILD_ROOT%{_includedir}/valgrind/vki
rm $RPM_BUILD_ROOT%{_libdir}/valgrind/*.a
%endif

# We don't want debuginfo generated for the vgpreload libraries.
# Turn off execute bit so they aren't included in the debuginfo.list.
# We'll turn the execute bit on again in %%files.
chmod 644 $RPM_BUILD_ROOT%{_libexecdir}/valgrind/vgpreload*-*-*so


%if %{with check}
%check
# Make sure a basic binary runs. There should be no errors.
./vg-in-place --error-exitcode=1 /bin/true --help

# Build the test files with the software collection compiler if available.
%{?scl:PATH=%{_bindir}${PATH:+:${PATH}}}
# Make sure no extra CFLAGS, CXXFLAGS or LDFLAGS leak through,
# the testsuite sets all flags necessary. See also configure above.
make %{?_smp_mflags} CFLAGS="" CXXFLAGS="" LDFLAGS="" check

# Workaround https://bugzilla.redhat.com/show_bug.cgi?id=1434601
# for gdbserver tests.
export PYTHONCOERCECLOCALE=0

echo ===============TESTING===================
%if 0%{?run_full_regtest}
  make regtest || :
%else
  make nonexp-regtest || :
%endif

# Make sure test failures show up in build.log
# Gather up the diffs (at most the first 20 lines for each one)
MAX_LINES=20
diff_files=`find */tests -name '*.diff*' | sort`
if [ z"$diff_files" = z ] ; then
   echo "Congratulations, all tests passed!" >> diffs
else
   for i in $diff_files ; do
      echo "=================================================" >> diffs
      echo $i                                                  >> diffs
      echo "=================================================" >> diffs
      if [ `wc -l < $i` -le $MAX_LINES ] ; then
         cat $i                                                >> diffs
      else
         head -n $MAX_LINES $i                                 >> diffs
         echo "<truncated beyond $MAX_LINES lines>"            >> diffs
      fi
   done
fi
cat diffs
echo ===============END TESTING===============
%endif


%clean
[ "$RPM_BUILD_ROOT" != "/" ] && rm -rf ${RPM_BUILD_ROOT}


%files
%defattr(-,root,root)
%license COPYING
%doc NEWS README_*
%doc docs/installed/html docs/installed/*.pdf
%{_bindir}/*
%dir %{_libexecdir}/%{name}
# Install everything in the libdir except the .so.
# The vgpreload so files might need file mode adjustment.
%{_libexecdir}/valgrind/*[^o]
# Turn on executable bit again for vgpreload libraries.
# Was disabled in %%install to prevent debuginfo stripping.
%attr(0755,root,root) %{_libexecdir}/valgrind/vgpreload*-*-*so
%{_mandir}/man1/*

%files devel
%defattr(-,root,root)
%dir %{_includedir}/valgrind
%{_includedir}/valgrind/valgrind.h
%{_includedir}/valgrind/cachegrind.h
%{_includedir}/valgrind/callgrind.h
%{_includedir}/valgrind/drd.h
%{_includedir}/valgrind/helgrind.h
%{_includedir}/valgrind/memcheck.h
%{_includedir}/valgrind/dhat.h
%{_libdir}/pkgconfig/valgrind.pc

%if %{build_tools_devel}
%files tools-devel
%{_includedir}/valgrind/config.h
%{_includedir}/valgrind/libvex*h
%{_includedir}/valgrind/pub_tool_*h
%{_includedir}/valgrind/vki
%dir %{_libdir}/valgrind
%{_libdir}/valgrind/*.a
%endif

%if %{build_openmpi}
%files openmpi
%defattr(-,root,root)
%dir %{_libdir}/valgrind
%{_libdir}/openmpi/valgrind/libmpiwrap*.so
%{_libdir}/valgrind/libmpiwrap*.so
%endif


%changelog
* Fri Jun 21 2024 Tomohiro "Tomo-p" KATO <tomop@teamgedoh.net> 3.23.0-1
- new upstream release.
- re-imported all patches from rawhide.

* Sat Sep 23 2023 Tomohiro "Tomo-p" KATO <tomop@teamgedoh.net> 3.21.0-1
- new upstream release.
- re-imported all patches from rawhide.

* Sat Sep 10 2022 Tomohiro "Tomo-p" KATO <tomop@teamgedoh.net> 3.19.0-1
- new upstream release.
- re-imported all patches from rawhide.

* Thu Aug 05 2021 Tomohiro "Tomo-p" KATO <tomop@teamgedoh.net> 3.17.0-2
- rebuilt with current environment.

* Sat Apr 10 2021 Tomohiro "Tomo-p" KATO <tomop@teamgedoh.net> 3.17.0-1
- new upstream release.
- dropped Patch4, 7-10.
- renumbered patches.

* Wed Feb 10 2021 Tomohiro "Tomo-p" KATO <tomop@teamgedoh.net> 3.16.1-2
- rebuilt with current environment.

* Wed Aug 19 2020 Tomohiro "Tomo-p" KATO <tomop@teamgedoh.net> 3.16.1-1
- new upstream release.
- dropped Patch4-35.
- import Patch4-10 from rawhide.

* Tue Jan 22 2019 Tomohiro "Tomo-p" KATO <tomop@teamgedoh.net> 3.14.0-1
- dropped Patch4-19.
- import Patch4-35 from rawhide.

* Fri Dec 22 2017 Tomohiro "Tomo-p" KATO <tomop@teamgedoh.net> 3.13.0-1
- updated to 3.13.0.
- replaced all Patches (from rawhide).

* Mon Mar 16 2015 Ryoichi INAGAKI <ryo1@toki.waseda.jp> 3.10.1-1
- updated valgrind to 3.10.1 based on fedora 3.10.1-6
- replaced all Patches (from fedora)

* Wed Apr 18 2012 Yoji TOYODA <bsyamato@sea.plala.or.jp> 3.7.0-1
- updated valgrind to 3.7.0
- replace all Patches (from fedora)

* Thu Nov 11 2010 Shu KONNO <owa@bg.wakwak.com> 3.6.0-1
- updated valgrind to 3.6.0
- updated BR: glibc-devel <= 2.12
- added BR: gcc <= 4.5
- added RQ: glibc <= 2.12

* Tue Oct 05 2010 Shu KONNO <owa@bg.wakwak.com> 3.5.0-1
- updated valgrind to 3.5.0
- added BR: glibc-devel >= 2.11
- split %{name}-devel
- added patches (from fedora core 12)
  - valgrind-3.5.0-cachegrind-improvements.patch
  - valgrind-3.5.0-openat.patch
  - valgrind-3.5.0-glibc-2.10.1.patch
  - valgrind-3.5.0-ifunc.patch
  - valgrind-3.5.0-inotify-init1.patch
  - valgrind-3.5.0-mmap-mprotect.patch
  - valgrind-3.5.0-dwarf3.patch
  - valgrind-3.5.0-pr40659.patch
  - valgrind-3.5.0-helgrind-race-supp.patch
  - valgrind-3.5.0-ppc-tests.patch
  - valgrind-3.5.0-amd64-loopnel.patch
  - valgrind-3.5.0-ppc-dwarf3.patch
  - valgrind-3.5.0-amd64-adcsbb.patch
  - valgrind-3.5.0-syscalls.patch
  - valgrind-3.5.0-preadv.patch
  - valgrind-3.5.0-glibc-2.11.patch

* Sun Apr 19 2009 Ryoichi INAGAKI <ryo1@bc.wakwak.com> 3.4.1-1vl5
- new upstream release
- changed Group to Development/Tools

* Sun Aug 24 2008 Shu KONNO <owa@bg.wakwak.com> 3.3.1-1vl5
- packaged new

