# 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

# 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

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

License: 	GPLv2+
Group: 		Development/Debuggers
URL: 		http://valgrind.org/

Source0: 	http://valgrind.org/downloads/%{name}-%{version}.tar.bz2

# Needs investigation and pushing upstream
Patch1: 	valgrind-3.9.0-cachegrind-improvements.patch
# KDE#211352 - helgrind races in helgrind's own mythread_wrapper
Patch2: 	valgrind-3.9.0-helgrind-race-supp.patch
# undef st_atime, st_mtime and st_ctime. Unknown why this is (still?) needed.
Patch3: 	valgrind-3.9.0-stat_h.patch
# Make ld.so supressions slightly less specific.
Patch4: 	valgrind-3.9.0-ldso-supp.patch
# KDE#342795 Internal glibc __GI_mempcpy call should be intercepted
Patch5: 	valgrind-3.10.1-mempcpy.patch
# KDE#343802 - s390x memcheck reports spurious conditional jump
Patch6: 	valgrind-3.10-s390-spechelper.patch
# KDE#342038, KDE#343732, KDE#343733, KDE#344007, KDE#344307
# mbind, get_mempolicy, set_mempolicy, flock, setgid, msgget, msgctl,
# msgrcv, msgsnd, accept4, mount, umount2
Patch7: 	valgrind-3.10.1-aarch64-syscalls.patch
# KDE#344007 ppc64 missing accept4 syscall
Patch8: 	valgrind-3.10.1-ppc64-accept4.patch
# KDE#344279 - syscall sendmmsg on arm64 (269) and ppc32/64 (349) unhandled
# KDE#344295 - syscall recvmmsg on arm64 (243) and ppc32/64 (343) unhandled
# KDE#344318 - socketcall should wrap recvmmsg and sendmmsg
Patch9: 	valgrind-3.10.1-send-recv-mmsg.patch
# Upstream valgrind svn r14530
Patch10: 	valgrind-3.10.1-glibc-version-check.patch

Buildroot: 	%{_tmppath}/%{name}-%{version}-root
%if %{build_multilib}
# Ensure glibc{,-devel} is installed for both multilib arches
BuildRequires: 	/lib/libc.so.6 /usr/lib/libc.so /lib64/libc.so.6 /usr/lib64/libc.so
%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:		Development/Libraries
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: 		Development/Debuggers
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.


%prep
%setup -q -n %{name}-%{version}
%patch1 -p1
%patch2 -p1
%patch3 -p1
%patch4 -p1
%patch5 -p1
%patch6 -p1
%patch7 -p1
%patch8 -p1
%patch9 -p1
%patch10 -p1


%build
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

%define mpiccpath %{_libdir}/openmpi/*/bin/mpicc

# 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.
OPTFLAGS="`echo " %{optflags} " | sed 's/ -m\(64\|3[21]\) / /g;s/ -fexceptions / /g;s/ -fstack-protector / / g;s/ -Wp,-D_FORTIFY_SOURCE=2 / /g;s/ -O2 / /g;s/ -mcpu=\([a-z0-9]\+\) / /g;s/^ //;s/ $//'`"
%configure CC="$CC" CFLAGS="$OPTFLAGS" CXXFLAGS="$OPTFLAGS" \
%if %{build_openmpi}
    --with-mpicc=%{mpiccpath} \
%endif
    GDB=%{_bindir}/gdb

make %{?_smp_mflags}

# Ensure there are no unexpected file descriptors open,
# the testsuite otherwise fails.
cat > close_fds.c <<EOF
#include <stdlib.h>
#include <unistd.h>
int main (int argc, char *const argv[])
{
  int i, j = sysconf (_SC_OPEN_MAX);
  if (j < 0)
    exit (1);
  for (i = 3; i < j; ++i)
    close (i);
  execvp (argv[1], argv + 1);
  exit (1);
}
EOF
gcc $RPM_OPT_FLAGS -o close_fds close_fds.c

%install
rm -rf $RPM_BUILD_ROOT
make install DESTDIR=$RPM_BUILD_ROOT

rm -rf docs.installed
mkdir docs.installed
mv $RPM_BUILD_ROOT%{_datadir}/doc/%{name}/* docs.installed/
rm -f docs.installed/*.ps

%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

# remove unuse files
rm -f $RPM_BUILD_ROOT%{_libdir}/valgrind/*.supp.in


%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 ; 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


%check
# Make sure a basic binary runs.
./vg-in-place /bin/true

# Make sure no extra CFLAGS leak through, the testsuite sets all flags
# necessary. See also configure above.
make %{?_smp_mflags} CFLAGS="" check || :

echo ===============TESTING===================
./close_fds make regtest || :

# 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 . -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===============


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

%files
%defattr(-,root,root)
%doc COPYING NEWS README_*
%doc docs.installed/html docs.installed/*.pdf
%{_bindir}/*
%dir %{_libdir}/%{name}
%{_libdir}/%{name}/*[^ao]
%{_libdir}/%{name}/[^l]*o
%{_mandir}/man1/*

%files devel
%defattr(-,root,root)
%{_includedir}/%{name}
%dir %{_libdir}/%{name}
%{_libdir}/%{name}/*.a
%{_libdir}/pkgconfig/%{name}.pc

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


%changelog
* 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

