1 # Copyright 1999-2024 Gentoo Authors
2 # Distributed under the terms of the GNU General Public License v2
6 PYTHON_COMPAT=( python3_{10..11} pypy3 )
8 inherit flag-o-matic fortran-2 python-any-r1 toolchain-funcs
10 DESCRIPTION="Portable, Extensible Toolkit for Scientific Computation"
11 HOMEPAGE="https://petsc.org/release/"
12 SRC_URI="https://web.cels.anl.gov/projects/petsc/download/release-snapshots/${P}.tar.gz"
16 KEYWORDS="~amd64 ~x86"
17 IUSE="afterimage boost complex-scalars debug +examples fftw
18 fortran hdf5 hypre int64 mpi metis mumps scotch superlu threads X"
20 # readd sparse when suitesparse-5.6.0 is in tree
21 # sparse? ( >=sci-libs/suitesparse-5.6.0 >=sci-libs/cholmod-1.7.0 )
22 # $(use_with sparse suitesparse) \
27 afterimage? ( media-libs/libafterimage )
28 boost? ( dev-libs/boost )
29 fftw? ( sci-libs/fftw:3.0[mpi?] )
30 hdf5? ( sci-libs/hdf5:=[mpi?] )
31 hypre? ( >=sci-libs/hypre-2.18.0[int64?,mpi?] )
32 metis? ( >=sci-libs/parmetis-4 )
33 mpi? ( virtual/mpi[fortran?] )
34 mumps? ( sci-libs/mumps[mpi?] sci-libs/scalapack )
35 scotch? ( sci-libs/scotch[int64?,mpi?] )
36 superlu? ( >=sci-libs/superlu-5 )
37 X? ( x11-libs/libX11 )
48 # hypre and superlu curretly exclude each other due to missing linking to hypre
52 complex-scalars? ( !hypre !superlu )
55 hypre? ( mpi !superlu )
61 "${FILESDIR}/${PN}-3.7.0-disable-rpath.patch"
62 "${FILESDIR}"/${PN}-3.16.0-fix_sandbox_violation.patch
65 # petsc uses --with-blah=1 and --with-blah=0 to en/disable options
67 use "$1" && echo "--with-${2:-$1}=1" || echo "--with-${2:-$1}=0"
69 # add external library:
70 # petsc_with use_flag libname libdir
71 # petsc_with use_flag libname include linking_libs
73 local myuse p=${2:-${1}}
76 if [[ $# -ge 4 ]]; then
77 myuse="${myuse} --with-${p}-include=${EPREFIX}${3}"
79 myuse="${myuse} --with-${p}-lib=$@"
81 myuse="${myuse} --with-${p}-dir=${EPREFIX}${3:-/usr}"
89 # select between configure options depending on use flag
91 use "$1" && echo "--with-$2=$3" || echo "--with-$2=$4"
95 # -Werror=lto-type-mismatch
96 # https://bugs.gentoo.org/863260
97 # https://gitlab.com/petsc/petsc/-/issues/1560
101 # PETSc runs mpi processes during configure that result in a sandbox
102 # violation by trying to open /proc/mtrr rw. This is not easy to
103 # mitigate because it happens in libpciaccess.so called by libhwloc.so,
104 # which is used by libmpi.so.
105 addpredict /proc/mtrr
106 # if mpi is built with knem support it needs /dev/knem too
109 # configureMPITypes with openmpi-2* insists on accessing the scaling
111 addpredict /sys/devices/system/cpu/
114 # configureMPIEXEC and configureMPITypes access /dev/nvidiactl
115 addpredict /dev/nvidiactl
122 use debug && myopt="debug" || myopt="opt"
124 # environmental variables expected by petsc during build
126 export PETSC_DIR="${S}"
127 export PETSC_ARCH="linux-gnu-c-${myopt}"
136 # C Support on CXX builds is enabled if possible i.e. when not using
137 # complex scalars (no complex type for both available at the same time)
142 CFLAGS="${CFLAGS} -fPIC" \
143 CPPFLAGS="${CPPFLAGS}" \
144 CXXFLAGS="${CXXFLAGS} -fPIC" \
145 CXXOPTFLAGS="${CXXFLAGS} -fPIC" \
146 FCFLAGS="${FCFLAGS} -fPIC" \
147 FFLAGS="${FFLAGS} -fPIC" \
148 LDFLAGS="${LDFLAGS}" \
149 MAKEFLAGS="${MAKEFLAGS}" \
151 --prefix="${EPREFIX}/usr/$(get_libdir)/petscdir/" \
152 --with-blas-lapack-lib="$($(tc-getPKG_CONFIG) --libs blas lapack)" \
153 --with-cmake:BOOL=1 \
154 --with-gnu-compilers \
155 --with-imagemagick=0 \
156 --with-petsc-arch="${PETSC_ARCH}" \
157 --with-precision=double \
159 --with-shared-libraries \
160 --with-single-library \
161 --with-windows-graphics=0 \
162 $(petsc_enable debug debugging) \
163 $(petsc_enable fortran) \
164 $(petsc_enable mpi) \
165 $(petsc_enable mpi mpi-compilers) \
166 $(petsc_enable threads pthread) \
167 $(petsc_select complex-scalars scalar-type complex real) \
168 $(petsc_select mpi cc mpicc $(tc-getCC)) \
169 $(petsc_select mpi cxx mpicxx $(tc-getCXX)) \
170 $(petsc_with afterimage afterimage /usr/include/libAfterImage -lAfterImage) \
171 $(petsc_with hypre hypre /usr/include/hypre -lHYPRE) \
172 $(petsc_with superlu superlu /usr/include/superlu -lsuperlu) \
173 $(petsc_with scotch ptscotch /usr/include/scotch [-lptesmumps,-lptscotch,-lptscotcherr,-lscotch,-lscotcherr]) \
174 $(petsc_with mumps scalapack /usr/include/scalapack -lscalapack) \
175 $(petsc_with mumps mumps /usr/include [-lcmumps,-ldmumps,-lsmumps,-lzmumps,-lmumps_common,-lpord]) \
176 $(use fortran && echo "$(petsc_select mpi fc mpif77 $(tc-getF77))") \
177 $(use int64 && echo "--with-index-size=64") \
186 emake DESTDIR="${ED}" install
192 # put all include directories under a proper subdirectory
193 mkdir "${ED}"/usr/include || die "mkdir failed (include)"
194 mv "${ED}"/usr/{$(get_libdir)/petscdir/include,include/petsc} || die "mv failed (include)"
196 # put libraries and pkconfig file into proper place
197 mv "${ED}"/usr/$(get_libdir)/petscdir/lib/{libpetsc*,pkgconfig} \
198 "${ED}/usr/$(get_libdir)" || die "mv failed (lib)"
200 # move share to proper location
201 mv "${ED}"/usr/{$(get_libdir)/petscdir/share,share} || die "mv failed (share)"
205 -e 's#include$#include/petsc#' \
206 -e "s#lib\$#$(get_libdir)#" \
207 -e "s#^prefix=.*petscdir\$#prefix=${EPREFIX}/usr#" \
208 "${ED}"/usr/$(get_libdir)/pkgconfig/*.pc || die "sed failed (pkgconfig)"
210 # recreate a "valid" petscdir:
211 for i in "${ED}"/usr/$(get_libdir)/*; do
212 [ $(basename $i) = petscdir ] && continue
213 ln -s "${EPREFIX}/usr/$(get_libdir)/$(basename $i)" \
214 "${ED}/usr/$(get_libdir)/petscdir/lib/$(basename $i)" || die "ln failed (petscdir)"
216 ln -s "${EPREFIX}"/usr/include/petsc/ \
217 "${ED}/usr/$(get_libdir)/petscdir/include" || die "ln failed (petscdir)"
218 mkdir "${ED}/usr/$(get_libdir)/petscdir/share" || die "mkdir fialed (petscdir)"
219 ln -s "${EPREFIX}"/usr/share/petsc/ \
220 "${ED}/usr/$(get_libdir)/petscdir/share/petsc" || die "ln failed (petscdir)"
222 # automatically symlink petsc matlab modules:
223 mkdir -p "${ED}"/usr/share/octave/site/m/
224 ln -s "${EPREFIX}"/usr/share/petsc/matlab "${ED}"/usr/share/octave/site/m/petsc || die "ln failed (matlab)"
226 if use examples; then
227 mkdir -p "${ED}"/usr/share/doc/${PF} || die "mkdir failed (examples)"
228 mv "${ED}"/usr/share/petsc/examples "${ED}"/usr/share/doc/${PF} || die "mv failed (examples)"
229 ln -s "${EPREFIX}"/usr/share/doc/${PF}/examples "${ED}"/usr/share/petsc/examples || die "ln failed (examples)"
230 docompress -x /usr/share/doc/${PF}/examples
232 rm -r "${ED}"/usr/share/petsc/examples || die "rm failed (examples)"
235 # add PETSC_DIR to environmental variables
236 cat >> 99petsc <<- EOF
237 PETSC_DIR=${EPREFIX}/usr/$(get_libdir)/petscdir