nixpkgs mirror (for testing)
github.com/NixOS/nixpkgs
nix
1{
2 lib,
3 newScope,
4 stdenv,
5 fetchzip,
6 replaceVars,
7 bash,
8 pkg-config,
9 gfortran,
10 bison,
11 mpi, # generic mpi dependency
12 mpiCheckPhaseHook,
13 python3Packages,
14
15 # Build options
16 debug ? false,
17 scalarType ? "real",
18 precision ? "double",
19 mpiSupport ? true,
20 fortranSupport ? true,
21 pythonSupport ? false, # petsc python binding
22 withExamples ? false,
23 withFullDeps ? false, # full External libraries support
24 withCommonDeps ? true, # common External libraries support
25
26 # External libraries options
27 withHdf5 ? withCommonDeps,
28 withMetis ? withCommonDeps,
29 withZlib ? (withP4est || withPtScotch),
30 withScalapack ? withCommonDeps && mpiSupport,
31 withParmetis ? withFullDeps, # parmetis is unfree
32 withPtScotch ? withCommonDeps && mpiSupport,
33 withMumps ? withCommonDeps,
34 withP4est ? withFullDeps,
35 withHypre ? withCommonDeps && mpiSupport,
36 withFftw ? withCommonDeps,
37 withSuperLu ? withCommonDeps,
38 withSuperLuDist ? withCommonDeps && mpiSupport,
39 withSuitesparse ? withCommonDeps,
40
41 # External libraries
42 blas,
43 lapack,
44 hdf5,
45 metis,
46 parmetis,
47 scotch,
48 scalapack,
49 mumps,
50 p4est,
51 zlib, # propagated by p4est but required by petsc
52 hypre,
53 fftw,
54 superlu,
55 superlu_dist,
56 suitesparse,
57
58 # Used in passthru.tests
59 petsc,
60 mpich,
61}:
62assert withFullDeps -> withCommonDeps;
63
64# This version of PETSc does not support a non-MPI p4est build
65assert withP4est -> (mpiSupport && withZlib);
66
67# Package parmetis depend on metis and mpi support
68assert withParmetis -> (withMetis && mpiSupport);
69
70assert withPtScotch -> (mpiSupport && withZlib);
71assert withScalapack -> mpiSupport;
72assert (withMumps && mpiSupport) -> withScalapack;
73assert withHypre -> mpiSupport;
74assert withSuperLuDist -> mpiSupport;
75
76let
77 petscPackages = lib.makeScope newScope (self: {
78 inherit
79 mpi
80 python3Packages
81 # global override options
82 mpiSupport
83 fortranSupport
84 pythonSupport
85 precision
86 withPtScotch
87 ;
88 enableMpi = self.mpiSupport;
89
90 petscPackages = self;
91 # external libraries
92 blas = self.callPackage blas.override { };
93 lapack = self.callPackage lapack.override { };
94 hdf5 = self.callPackage hdf5.override {
95 fortran = gfortran;
96 cppSupport = !mpiSupport;
97 };
98 metis = self.callPackage metis.override { };
99 parmetis = self.callPackage parmetis.override { };
100 scotch = self.callPackage scotch.override { };
101 scalapack = self.callPackage scalapack.override { };
102 mumps = self.callPackage mumps.override { };
103 p4est = self.callPackage p4est.override { };
104 hypre = self.callPackage hypre.override { };
105 fftw = self.callPackage fftw.override { };
106 superlu = self.callPackage superlu.override { };
107 superlu_dist = self.callPackage superlu_dist.override { };
108 suitesparse = self.callPackage suitesparse.override { };
109 });
110in
111stdenv.mkDerivation (finalAttrs: {
112 pname = "petsc";
113 version = "3.24.4";
114
115 src = fetchzip {
116 url = "https://web.cels.anl.gov/projects/petsc/download/release-snapshots/petsc-${finalAttrs.version}.tar.gz";
117 hash = "sha256-bU6CLYRbzXRUjh4J4sadaemPpmM6q6/3n6J9H0MD6hs=";
118 };
119
120 patches = [
121 (replaceVars ./fix-petsc4py-install-prefix.patch {
122 PYTHON_SITEPACKAGES = python3Packages.python.sitePackages;
123 })
124 ];
125
126 postPatch = ''
127 patchShebangs ./lib/petsc/bin
128
129 substituteInPlace config/example_template.py \
130 --replace-fail "/usr/bin/env bash" "${bash}/bin/bash"
131 '';
132
133 strictDeps = true;
134
135 nativeBuildInputs = [
136 gfortran
137 pkg-config
138 bison
139 python3Packages.python
140 ]
141 ++ lib.optional mpiSupport mpi
142 ++ lib.optionals pythonSupport [
143 python3Packages.setuptools
144 python3Packages.cython
145 ];
146
147 buildInputs = [
148 petscPackages.blas
149 petscPackages.lapack
150 ]
151 ++ lib.optional withZlib zlib
152 ++ lib.optional withHdf5 petscPackages.hdf5
153 ++ lib.optional withP4est petscPackages.p4est
154 ++ lib.optional withMetis petscPackages.metis
155 ++ lib.optional withParmetis petscPackages.parmetis
156 ++ lib.optional withPtScotch petscPackages.scotch
157 ++ lib.optional withScalapack petscPackages.scalapack
158 ++ lib.optional withMumps petscPackages.mumps
159 ++ lib.optional withHypre petscPackages.hypre
160 ++ lib.optional withSuperLu petscPackages.superlu
161 ++ lib.optional withSuperLuDist petscPackages.superlu_dist
162 ++ lib.optional withFftw petscPackages.fftw
163 ++ lib.optional withSuitesparse petscPackages.suitesparse;
164
165 propagatedBuildInputs = lib.optional pythonSupport python3Packages.numpy;
166
167 configureFlags = [
168 "--with-blaslapack=1"
169 "--with-scalar-type=${scalarType}"
170 "--with-precision=${precision}"
171 "--with-mpi=${if mpiSupport then "1" else "0"}"
172 ]
173 ++ lib.optionals (!mpiSupport) [
174 "--with-cc=${stdenv.cc}/bin/${if stdenv.cc.isGNU then "gcc" else "clang"}"
175 "--with-cxx=${stdenv.cc}/bin/${if stdenv.cc.isGNU then "g++" else "clang++"}"
176 "--with-fc=${gfortran}/bin/gfortran"
177 ]
178 ++ lib.optionals mpiSupport [
179 "--with-cc=${lib.getDev mpi}/bin/mpicc"
180 "--with-cxx=${lib.getDev mpi}/bin/mpicxx"
181 "--with-fc=${lib.getDev mpi}/bin/mpif90"
182 ]
183 ++ lib.optional (!debug) "--with-debugging=0"
184 ++ lib.optional (!fortranSupport) "--with-fortran-bindings=0"
185 ++ lib.optional pythonSupport "--with-petsc4py=1"
186 ++ lib.optional withMetis "--with-metis=1"
187 ++ lib.optional withParmetis "--with-parmetis=1"
188 ++ lib.optional withPtScotch "--with-ptscotch=1"
189 ++ lib.optional withScalapack "--with-scalapack=1"
190 ++ lib.optional withMumps "--with-mumps=1"
191 ++ lib.optional (withMumps && !mpiSupport) "--with-mumps-serial=1"
192 ++ lib.optional withP4est "--with-p4est=1"
193 ++ lib.optional withZlib "--with-zlib=1"
194 ++ lib.optional withHdf5 "--with-hdf5=1"
195 ++ lib.optional withHypre "--with-hypre=1"
196 ++ lib.optional withSuperLu "--with-superlu=1"
197 ++ lib.optional withSuperLuDist "--with-superlu_dist=1"
198 ++ lib.optional withFftw "--with-fftw=1"
199 ++ lib.optional withSuitesparse "--with-suitesparse=1";
200
201 installTargets = [ (if withExamples then "install" else "install-lib") ];
202
203 enableParallelBuilding = true;
204
205 # Ensure petscvariables contains absolute paths for compilers and flags so that downstream
206 # packages relying on PETSc's runtime configuration (e.g. form compilers, code generators)
207 # can correctly compile and link generated code
208 postInstall = lib.concatStringsSep "\n" (
209 map (
210 package:
211 let
212 pname = package.pname or package.name;
213 prefix =
214 if (pname == "blas" || pname == "lapack") then
215 "BLASLAPACK"
216 else
217 lib.toUpper (builtins.elemAt (lib.splitString "-" pname) 0);
218 in
219 ''
220 substituteInPlace $out/lib/petsc/conf/petscvariables \
221 --replace-fail "${prefix}_INCLUDE =" "${prefix}_INCLUDE = -I${lib.getDev package}/include" \
222 --replace-fail "${prefix}_LIB =" "${prefix}_LIB = -L${lib.getLib package}/lib"
223 ''
224 ) finalAttrs.buildInputs
225 );
226
227 __darwinAllowLocalNetworking = true;
228
229 # This is needed as the checks need to compile and link the test cases with
230 # -lpetsc, which is not available in the checkPhase, which is executed before
231 # the installPhase. The installCheckPhase comes after the installPhase, so
232 # the library is installed and available.
233 doInstallCheck = true;
234 installCheckTarget = "check_install";
235
236 # The PETSC4PY=no flag disables the ex100 test,
237 # which compiles C code to load Python modules for solving a math problem.
238 # This test fails on the Darwin platform but is rarely a common use case for petsc4py.
239 installCheckFlags = lib.optional stdenv.hostPlatform.isDarwin "PETSC4PY=no";
240
241 nativeInstallCheckInputs = [
242 mpiCheckPhaseHook
243 ]
244 ++ lib.optionals pythonSupport [
245 python3Packages.pythonImportsCheckHook
246 python3Packages.unittestCheckHook
247 ];
248
249 unittestFlagsArray = [
250 "-s"
251 "src/binding/petsc4py/test"
252 "-v"
253 ];
254
255 pythonImportsCheck = [ "petsc4py" ];
256
257 passthru = {
258 inherit
259 mpiSupport
260 pythonSupport
261 fortranSupport
262 ;
263 petscPackages = petscPackages.overrideScope (
264 final: prev: {
265 petsc = finalAttrs.finalPackage;
266 }
267 );
268 tests = {
269 serial = petsc.override {
270 mpiSupport = false;
271 };
272 }
273 // lib.optionalAttrs stdenv.hostPlatform.isLinux {
274 fullDeps = petsc.override {
275 withFullDeps = true;
276 withParmetis = false;
277 };
278 mpich = petsc.override {
279 mpi = mpich;
280 };
281 };
282 };
283
284 setupHook = ./setup-hook.sh;
285
286 meta = {
287 description = "Portable Extensible Toolkit for Scientific computation";
288 homepage = "https://petsc.org/release/";
289 license = lib.licenses.bsd2;
290 platforms = lib.platforms.unix;
291 maintainers = with lib.maintainers; [ qbisi ];
292 };
293})