%undefine       _ld_as_needed

%if %{?__isa_bits:%{__isa_bits}}%{!?__isa_bits:32} == 64
%global         arch64 1
%else
%global         arch64 0
%endif

Name:           metis
Version:        5.1.0
Release:        2
Summary:        Serial Graph Partitioning and Fill-reducing Matrix Ordering
License:        Apache-2.0
URL:            http://glaros.dtc.umn.edu/gkhome/views/%{name}
Source0:        http://glaros.dtc.umn.edu/gkhome/fetch/sw/%{name}/%{name}-%{version}.tar.gz

Patch0:         %{name}-libmetis.patch
Patch1:         %{name}-shared-GKlib.patch
Patch2:         %{name}-width-datatype.patch
Patch3:         %{name}-GKREGEX-GKRAND-LIBSUFFIX-fix.patch
Patch4:         %{name}_lib64.patch
Patch5:         %{name}-pcre2.patch

BuildRequires:  make cmake gcc gcc-c++ pcre2-devel help2man chrpath

%description
METIS is a set of serial programs for partitioning graphs, 
partitioning finite element meshes, and producing fill reducing 
orderings for sparse matrices. 
The algorithms implemented in METIS are based on the multilevel 
recursive-bisection, multilevel k-way, and multi-constraint 
partitioning schemes developed in our lab.
METIS is distributed with OpenMP support.


%package        devel
Summary:        METIS headers and development-related files
Requires:       %{name}%{?_isa} = %{version}-%{release}

%description    devel
Header and library files of Metis.


%if 0%{?arch64}
%package        -n metis64
Summary:        Serial Graph Partitioning and Fill-reducing Matrix Ordering (64bit INTEGER)

%description    -n metis64
METIS is a set of serial programs for partitioning graphs, 
partitioning finite element meshes, and producing fill reducing 
orderings for sparse matrices. 
The algorithms implemented in METIS are based on the multilevel 
recursive-bisection, multilevel k-way, and multi-constraint 
partitioning schemes developed in our lab.
METIS is distributed with OpenMP support.
This build has 64bit INTEGER support.


%package        -n metis64-devel
Summary:        METIS development libraries (64bit INTEGER)
Requires:       metis64%{?_isa} = %{version}-%{release}

%description    -n metis64-devel
Header and library files of Metis,
OpenMP version (64bit INTEGER).
%endif

%prep
%setup -qc 
 
pushd %{name}-%{version}
%patch0 -p0
%patch1 -p0
%patch3 -p0
%patch5 -p0

## Remove default compiler flag
sed -e 's|-O3||g' -i GKlib/GKlibSystem.cmake
popd

%if 0%{?arch64}
%{__cp} -a %{name}-%{version} metis64
%endif


%build
PCRE_LDFLAGS="-lpcre2-posix"

%cmake -S %{name}-%{version} -B %{name}-%{version}/build \
    -DCMAKE_BUILD_TYPE:STRING=Release \
    -DGKLIB_PATH=%{name}-%{version}/GKlib  \
    -DSHARED:BOOL=TRUE \
    -DOPENMP:BOOL=ON \
    -DPCRE:BOOL=ON \
    -DCMAKE_C_FLAGS:STRING="%{optflags} -pthread" \
    -DCMAKE_SHARED_LINKER_FLAGS_RELEASE:STRING="%{__global_ldflags} $PCRE_LDFLAGS" \
    -DCMAKE_EXE_LINKER_FLAGS_RELEASE:STRING="%{__global_ldflags} $PCRE_LDFLAGS" \
    -DCMAKE_VERBOSE_MAKEFILE:BOOL=TRUE \
    -DCMAKE_INSTALL_PREFIX:PATH=%{_prefix}

%make_build -C %{name}-%{version}/build

%if 0%{?arch64}
cd metis64
patch -p0 < %{PATCH2}
patch -p0 < %{PATCH4}
cd ..
mkdir -p metis64/build64
PCRE_LDFLAGS="-lpcre2-posix"

%cmake -S metis64 -B metis64/build64 \
    -DCMAKE_BUILD_TYPE:STRING=Release \
    -DGKLIB_PATH=metis64/GKlib  \
    -DSHARED:BOOL=TRUE \
    -DOPENMP:BOOL=ON \
    -DPCRE:BOOL=ON \
    -DCMAKE_C_FLAGS:STRING="%{optflags} -pthread" \
    -DCMAKE_SHARED_LINKER_FLAGS_RELEASE:STRING="%{__global_ldflags} $PCRE_LDFLAGS" \
    -DCMAKE_EXE_LINKER_FLAGS_RELEASE:STRING="%{__global_ldflags} $PCRE_LDFLAGS" \
    -DCMAKE_VERBOSE_MAKEFILE:BOOL=TRUE \
    -DCMAKE_INSTALL_PREFIX:PATH=%{_prefix} ..

%make_build -C metis64/build64
%endif


%install
pushd %{name}-%{version}/build
%make_install

## Generate manpages from binaries
LD_PRELOAD=%{buildroot}%{_libdir}/lib%{name}.so.0 \
help2man --version-string="%{version}" -n "Partitions a graph into a specified number of parts." \
    -N --output="gpmetis.1" --no-discard-stderr --help-option="-help" %{buildroot}%{_bindir}/gpmetis

LD_PRELOAD=%{buildroot}%{_libdir}/lib%{name}.so.0 \
help2man --version-string="%{version}" \
    -n "Computes a fill-reducing ordering of the vertices of the graph using multilevel nested dissection." \
    -N --output="ndmetis.1" --no-discard-stderr --help-option="-help" %{buildroot}%{_bindir}/ndmetis

LD_PRELOAD=%{buildroot}%{_libdir}/lib%{name}.so.0 \
help2man --version-string="%{version}" -n "Partitions a mesh into a specified number of parts." \
    -N --output="mpmetis.1" --no-discard-stderr --help-option="-help" %{buildroot}%{_bindir}/mpmetis

LD_PRELOAD=%{buildroot}%{_libdir}/lib%{name}.so.0 \
help2man --version-string="%{version}" -n "Converts a mesh into a graph that is compatible with METIS." \
    -N --output="m2gmetis.1" --no-discard-stderr -h "-help" %{buildroot}%{_bindir}/m2gmetis

%{__mkdir_p} %{buildroot}%{_mandir}/man1
%{__mv} *.1 %{buildroot}%{_mandir}/man1
popd

# Save metis.h with IDXTYPEWIDTH = 32
%{__mv} %{buildroot}%{_includedir}/metis.h %{buildroot}%{_includedir}/metis32.h

%if 0%{?arch64}
pushd metis64/build64
%make_install
# Save metis.h with IDXTYPEWIDTH = 64
%{__mv} %{buildroot}%{_includedir}/metis.h %{buildroot}%{_includedir}/metis64.h
popd
%endif

# Save metis.h with IDXTYPEWIDTH = 32
%{__mv} %{buildroot}%{_includedir}/metis32.h %{buildroot}%{_includedir}/metis.h

## Remove rpaths
chrpath -d %{buildroot}%{_bindir}/*


%check
%{__cp} -p %{buildroot}%{_bindir}/* %{name}-%{version}/graphs
pushd %{name}-%{version}/graphs
LD_LIBRARY_PATH=%{buildroot}%{_libdir}:$LD_LIBRARY_PATH ./ndmetis mdual.graph
LD_LIBRARY_PATH=%{buildroot}%{_libdir}:$LD_LIBRARY_PATH ./mpmetis metis.mesh 2
LD_LIBRARY_PATH=%{buildroot}%{_libdir}:$LD_LIBRARY_PATH ./gpmetis test.mgraph 4
LD_LIBRARY_PATH=%{buildroot}%{_libdir}:$LD_LIBRARY_PATH ./gpmetis copter2.graph 4
LD_LIBRARY_PATH=%{buildroot}%{_libdir}:$LD_LIBRARY_PATH ./graphchk 4elt.graph

%if 0%{?arch64}
LD_LIBRARY_PATH=%{buildroot}%{_libdir}:$LD_LIBRARY_PATH ./ndmetis64 mdual.graph
LD_LIBRARY_PATH=%{buildroot}%{_libdir}:$LD_LIBRARY_PATH ./mpmetis64 metis.mesh 2
LD_LIBRARY_PATH=%{buildroot}%{_libdir}:$LD_LIBRARY_PATH ./gpmetis64 test.mgraph 4
LD_LIBRARY_PATH=%{buildroot}%{_libdir}:$LD_LIBRARY_PATH ./gpmetis64 copter2.graph 4
LD_LIBRARY_PATH=%{buildroot}%{_libdir}:$LD_LIBRARY_PATH ./graphchk64 4elt.graph
%endif
popd


%files
%doc %{name}-%{version}/Changelog %{name}-%{version}/manual/manual.pdf
%license %{name}-%{version}/LICENSE.txt
%{_bindir}/cmpfillin
%{_bindir}/gpmetis
%{_bindir}/graphchk
%{_bindir}/m2gmetis
%{_bindir}/mpmetis
%{_bindir}/ndmetis
%{_mandir}/man1/*.1.gz
%{_libdir}/lib%{name}.so.*


%files devel
%{_includedir}/%{name}.h
%{_libdir}/lib%{name}.so


%if 0%{?arch64}
%files -n metis64
%doc metis64/Changelog metis64/manual/manual.pdf
%license metis64/LICENSE.txt
%{_bindir}/cmpfillin64
%{_bindir}/gpmetis64
%{_bindir}/graphchk64
%{_bindir}/m2gmetis64
%{_bindir}/mpmetis64
%{_bindir}/ndmetis64
%{_libdir}/lib%{name}64.so.*


%files -n metis64-devel
%{_includedir}/%{name}64.h
%{_libdir}/lib%{name}64.so
%endif


%changelog
* Tue May 10 2022 caodongxia <caodongxia@h-partners.com> - 5.1.0-2
- License compliance rectification

* Mon Dec 20 2021 herengui <herengui@uniontech.com> - 5.1.0-1
- Initial package
