From d5e08421c26976f0077b902cb6a78484bc5b3ccd Mon Sep 17 00:00:00 2001 From: qbisi Date: Sun, 20 Apr 2025 01:08:34 +0800 Subject: [PATCH] petsc: add compile and link flags in lib/petsc/conf/petscvariables --- pkgs/by-name/pe/petsc/package.nix | 33 ++++++++++++++++++++++++++++--- 1 file changed, 30 insertions(+), 3 deletions(-) diff --git a/pkgs/by-name/pe/petsc/package.nix b/pkgs/by-name/pe/petsc/package.nix index 322def2b2fe5..7dc61b709db3 100644 --- a/pkgs/by-name/pe/petsc/package.nix +++ b/pkgs/by-name/pe/petsc/package.nix @@ -171,10 +171,15 @@ stdenv.mkDerivation (finalAttrs: { "--with-precision=${precision}" "--with-mpi=${if mpiSupport then "1" else "0"}" ] + ++ lib.optionals (!mpiSupport) [ + "--with-cc=${stdenv.cc}/bin/${if stdenv.cc.isGNU then "gcc" else "clang"}" + "--with-cxx=${stdenv.cc}/bin/${if stdenv.cc.isGNU then "g++" else "clang++"}" + "--with-fc=${gfortran}/bin/gfortran" + ] ++ lib.optionals mpiSupport [ - "--CC=mpicc" - "--with-cxx=mpicxx" - "--with-fc=mpif90" + "--with-cc=${lib.getDev mpi}/bin/mpicc" + "--with-cxx=${lib.getDev mpi}/bin/mpicxx" + "--with-fc=${lib.getDev mpi}/bin/mpif90" ] ++ lib.optionals (!debug) [ "--with-debugging=0" @@ -209,6 +214,28 @@ stdenv.mkDerivation (finalAttrs: { enableParallelBuilding = true; + # Ensure petscvariables contains absolute paths for compilers and flags so that downstream + # packages relying on PETSc's runtime configuration (e.g. form compilers, code generators) + # can correctly compile and link generated code + postInstall = lib.concatStringsSep "\n" ( + map ( + package: + let + pname = package.pname or package.name; + prefix = + if (pname == "blas" || pname == "lapack") then + "BLASLAPACK" + else + lib.toUpper (builtins.elemAt (lib.splitString "-" pname) 0); + in + '' + substituteInPlace $out/lib/petsc/conf/petscvariables \ + --replace-fail "${prefix}_INCLUDE =" "${prefix}_INCLUDE = -I${lib.getDev package}/include" \ + --replace-fail "${prefix}_LIB =" "${prefix}_LIB = -L${lib.getLib package}/lib" + '' + ) finalAttrs.buildInputs + ); + # This is needed as the checks need to compile and link the test cases with # -lpetsc, which is not available in the checkPhase, which is executed before # the installPhase. The installCheckPhase comes after the installPhase, so