We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
Unable to build PETSc, PETSC_DIR already set
`configure` fails immediately if the `PETSC_DIR` environment variable is already set. Spack should unset this.
```
===============================================================================
             Configuring PETSc to compile on your system                       
===============================================================================
TESTING: configureExternalPackagesDir from config.framework(/scratch/ajstewart/spack-stage/spack-stage-sFRYcv/petsc-3.7.4/config/BuildSystem/config/framework.py:834)
TESTING: configureDebuggers from config.utilities.debuggers(/scratch/ajstewart/spack-stage/spack-stage-sFRYcv/petsc-3.7.4/config/BuildSystem/config/utilities/debuggers.py:22)
TESTING: configureGit from config.sourceControl(/scratch/ajstewart/spack-stage/spack-stage-sFRYcv/petsc-3.7.4/config/BuildSystem/config/sourceControl.py:24)
TESTING: configureMercurial from config.sourceControl(/scratch/ajstewart/spack-stage/spack-stage-sFRYcv/petsc-3.7.4/config/BuildSystem/config/sourceControl.py:35)
TESTING: configureCLanguage from PETSc.options.languages(/scratch/ajstewart/spack-stage/spack-stage-sFRYcv/petsc-3.7.4/config/PETSc/options/languages.py:27)
TESTING: configureDirectories from PETSc.options.petscdir(/scratch/ajstewart/spack-stage/spack-stage-sFRYcv/petsc-3.7.4/config/PETSc/options/petscdir.py:23)
*******************************************************************************
         UNABLE to CONFIGURE with GIVEN OPTIONS    (see configure.log for details):
-------------------------------------------------------------------------------
The environmental variable PETSC_DIR /home/ajstewart/petsc MUST be the current directory /scratch/ajstewart/spack-stage/spack-stage-sFRYcv/petsc-3.7.4
*******************************************************************************
```

--- END ISSUE ---

Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
--- BEGIN FILES ---
Path: `var/spack/repos/builtin/packages/petsc/package.py`
Content:
```
1 ##############################################################################
2 # Copyright (c) 2013-2016, Lawrence Livermore National Security, LLC.
3 # Produced at the Lawrence Livermore National Laboratory.
4 #
5 # This file is part of Spack.
6 # Created by Todd Gamblin, tgamblin@llnl.gov, All rights reserved.
7 # LLNL-CODE-647188
8 #
9 # For details, see https://github.com/llnl/spack
10 # Please also see the LICENSE file for our notice and the LGPL.
11 #
12 # This program is free software; you can redistribute it and/or modify
13 # it under the terms of the GNU Lesser General Public License (as
14 # published by the Free Software Foundation) version 2.1, February 1999.
15 #
16 # This program is distributed in the hope that it will be useful, but
17 # WITHOUT ANY WARRANTY; without even the IMPLIED WARRANTY OF
18 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the terms and
19 # conditions of the GNU Lesser General Public License for more details.
20 #
21 # You should have received a copy of the GNU Lesser General Public
22 # License along with this program; if not, write to the Free Software
23 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 ##############################################################################
25 
26 import os
27 import sys
28 from spack import *
29 
30 
31 class Petsc(Package):
32     """PETSc is a suite of data structures and routines for the scalable
33     (parallel) solution of scientific applications modeled by partial
34     differential equations.
35     """
36 
37     homepage = "http://www.mcs.anl.gov/petsc/index.html"
38     url = "http://ftp.mcs.anl.gov/pub/petsc/release-snapshots/petsc-3.5.3.tar.gz"
39 
40     version('develop', git='https://bitbucket.org/petsc/petsc.git', tag='master')
41     version('for-pflotran-0.1.0', git='https://bitbucket.org/petsc/petsc.git',
42             commit='7943f4e1472fff9cf1fc630a1100136616e4970f')
43 
44     version('3.7.4', 'aaf94fa54ef83022c14091f10866eedf')
45     version('3.7.2', '50da49867ce7a49e7a0c1b37f4ec7b34')
46     version('3.6.4', '7632da2375a3df35b8891c9526dbdde7')
47     version('3.6.3', '91dd3522de5a5ef039ff8f50800db606')
48     version('3.5.3', 'd4fd2734661e89f18ac6014b5dd1ef2f')
49     version('3.5.2', 'ad170802b3b058b5deb9cd1f968e7e13')
50     version('3.5.1', 'a557e029711ebf425544e117ffa44d8f')
51     version('3.4.4', '7edbc68aa6d8d6a3295dd5f6c2f6979d')
52 
53     variant('shared',  default=True,
54             description='Enables the build of shared libraries')
55     variant('mpi',     default=True,  description='Activates MPI support')
56     variant('double',  default=True,
57             description='Switches between single and double precision')
58     variant('complex', default=False, description='Build with complex numbers')
59     variant('debug',   default=False, description='Compile in debug mode')
60 
61     variant('metis',   default=True,
62             description='Activates support for metis and parmetis')
63     variant('hdf5',    default=True,
64             description='Activates support for HDF5 (only parallel)')
65     variant('boost',   default=True,  description='Activates support for Boost')
66     variant('hypre',   default=True,
67             description='Activates support for Hypre (only parallel)')
68     variant('mumps',   default=True,
69             description='Activates support for MUMPS (only parallel)')
70     variant('superlu-dist', default=True,
71             description='Activates support for SuperluDist (only parallel)')
72 
73     # Virtual dependencies
74     # Git repository needs sowing to build Fortran interface
75     depends_on('sowing', when='@develop')
76 
77     depends_on('blas')
78     depends_on('lapack')
79     depends_on('mpi', when='+mpi')
80 
81     # Build dependencies
82     depends_on('python @2.6:2.7', type='build')
83 
84     # Other dependencies
85     depends_on('boost', when='@:3.5+boost')
86     depends_on('metis@5:', when='+metis')
87 
88     depends_on('hdf5+mpi', when='+hdf5+mpi')
89     depends_on('parmetis', when='+metis+mpi')
90     # Hypre does not support complex numbers.
91     # Also PETSc prefer to build it without internal superlu, likely due to
92     # conflict in headers see
93     # https://bitbucket.org/petsc/petsc/src/90564b43f6b05485163c147b464b5d6d28cde3ef/config/BuildSystem/config/packages/hypre.py
94     depends_on('hypre~internal-superlu', when='+hypre+mpi~complex')
95     depends_on('superlu-dist@:4.3', when='@3.4.4:3.6.4+superlu-dist+mpi')
96     depends_on('superlu-dist@5.0.0:', when='@3.7:+superlu-dist+mpi')
97     depends_on('superlu-dist@5.0.0:', when='@for-pflotran-0.1.0+superlu-dist+mpi')
98     depends_on('mumps+mpi', when='+mumps+mpi')
99     depends_on('scalapack', when='+mumps+mpi')
100 
101     def mpi_dependent_options(self):
102         if '~mpi' in self.spec:
103             compiler_opts = [
104                 '--with-cc=%s' % os.environ['CC'],
105                 '--with-cxx=%s' % (os.environ['CXX']
106                                    if self.compiler.cxx is not None else '0'),
107                 '--with-fc=%s' % (os.environ['FC']
108                                   if self.compiler.fc is not None else '0'),
109                 '--with-mpi=0'
110             ]
111             error_message_fmt = \
112                 '\t{library} support requires "+mpi" to be activated'
113 
114             # If mpi is disabled (~mpi), it's an error to have any of these
115             # enabled. This generates a list of any such errors.
116             errors = [
117                 error_message_fmt.format(library=x)
118                 for x in ('hdf5', 'hypre', 'parmetis', 'mumps', 'superlu-dist')
119                 if ('+' + x) in self.spec]
120             if errors:
121                 errors = ['incompatible variants given'] + errors
122                 raise RuntimeError('\n'.join(errors))
123         else:
124             compiler_opts = [
125                 '--with-mpi=1',
126                 '--with-mpi-dir=%s' % self.spec['mpi'].prefix,
127             ]
128         if sys.platform != "darwin":
129             compiler_opts.extend([
130                 '--with-cpp=cpp',
131                 '--with-cxxcpp=cpp',
132             ])
133         return compiler_opts
134 
135     def install(self, spec, prefix):
136         options = ['--with-ssl=0',
137                    '--with-x=0',
138                    '--download-c2html=0',
139                    '--download-sowing=0',
140                    '--download-hwloc=0']
141         options.extend(self.mpi_dependent_options())
142         options.extend([
143             '--with-precision=%s' % (
144                 'double' if '+double' in spec else 'single'),
145             '--with-scalar-type=%s' % (
146                 'complex' if '+complex' in spec else 'real'),
147             '--with-shared-libraries=%s' % ('1' if '+shared' in spec else '0'),
148             '--with-debugging=%s' % ('1' if '+debug' in spec else '0')
149         ])
150         # Make sure we use exactly the same Blas/Lapack libraries
151         # across the DAG. To that end list them explicitly
152         lapack_blas = spec['lapack'].lapack_libs + spec['blas'].blas_libs
153         options.extend([
154             '--with-blas-lapack-lib=%s' % lapack_blas.joined()
155         ])
156 
157         # Activates library support if needed
158         for library in ('metis', 'boost', 'hdf5', 'hypre', 'parmetis',
159                         'mumps', 'scalapack'):
160             options.append(
161                 '--with-{library}={value}'.format(
162                     library=library, value=('1' if library in spec else '0'))
163             )
164             if library in spec:
165                 options.append(
166                     '--with-{library}-dir={path}'.format(
167                         library=library, path=spec[library].prefix)
168                 )
169         # PETSc does not pick up SuperluDist from the dir as they look for
170         # superlu_dist_4.1.a
171         if 'superlu-dist' in spec:
172             options.extend([
173                 '--with-superlu_dist-include=%s' %
174                 spec['superlu-dist'].prefix.include,
175                 '--with-superlu_dist-lib=%s' %
176                 join_path(spec['superlu-dist'].prefix.lib,
177                           'libsuperlu_dist.a'),
178                 '--with-superlu_dist=1'
179             ])
180         else:
181             options.append(
182                 '--with-superlu_dist=0'
183             )
184 
185         configure('--prefix=%s' % prefix, *options)
186 
187         # PETSc has its own way of doing parallel make.
188         make('MAKE_NP=%s' % make_jobs, parallel=False)
189         make("install")
190 
191         # solve Poisson equation in 2D to make sure nothing is broken:
192         if ('mpi' in spec) and self.run_tests:
193             with working_dir('src/ksp/ksp/examples/tutorials'):
194                 env['PETSC_DIR'] = self.prefix
195                 cc = Executable(spec['mpi'].mpicc)
196                 cc('ex50.c', '-I%s' % prefix.include, '-L%s' % prefix.lib,
197                    '-lpetsc', '-lm', '-o', 'ex50')
198                 run = Executable(join_path(spec['mpi'].prefix.bin, 'mpirun'))
199                 run('ex50', '-da_grid_x', '4', '-da_grid_y', '4')
200                 if 'superlu-dist' in spec:
201                     run('ex50',
202                         '-da_grid_x', '4',
203                         '-da_grid_y', '4',
204                         '-pc_type', 'lu',
205                         '-pc_factor_mat_solver_package', 'superlu_dist')
206 
207                 if 'mumps' in spec:
208                     run('ex50',
209                         '-da_grid_x', '4',
210                         '-da_grid_y', '4',
211                         '-pc_type', 'lu',
212                         '-pc_factor_mat_solver_package', 'mumps')
213 
214                 if 'hypre' in spec:
215                     run('ex50',
216                         '-da_grid_x', '4',
217                         '-da_grid_y', '4',
218                         '-pc_type', 'hypre',
219                         '-pc_hypre_type', 'boomeramg')
220 
221     def setup_dependent_environment(self, spack_env, run_env, dependent_spec):
222         # set up PETSC_DIR for everyone using PETSc package
223         spack_env.set('PETSC_DIR', self.prefix)
224
```
--- END FILES ---

Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.

Here is an example:

```diff
diff --git a/examples/server_async.py b/examples/server_async.py
--- a/examples/server_async.py
+++ b/examples/server_async.py
@@ -313,4 +313,4 @@
 
 
 if __name__ == "__main__":
-    asyncio.run(run_async_server("."), debug=True)
+    asyncio.run(run_async_server(), debug=True)
diff --git a/examples/server_sync.py b/examples/server_sync.py
--- a/examples/server_sync.py
+++ b/examples/server_sync.py
@@ -313,5 +313,5 @@
 
 
 if __name__ == "__main__":
-    server = run_sync_server(".")
+    server = run_sync_server()
     server.shutdown()

```

diff --git a/var/spack/repos/builtin/packages/petsc/package.py b/var/spack/repos/builtin/packages/petsc/package.py
--- a/var/spack/repos/builtin/packages/petsc/package.py
+++ b/var/spack/repos/builtin/packages/petsc/package.py
@@ -218,6 +218,16 @@
                         '-pc_type', 'hypre',
                         '-pc_hypre_type', 'boomeramg')
 
+    def setup_environment(self, spack_env, run_env):
+        # configure fails if these env vars are set outside of Spack
+        spack_env.unset('PETSC_DIR')
+        spack_env.unset('PETSC_ARCH')
+
+        # Set PETSC_DIR in the module file
+        run_env.set('PETSC_DIR', self.prefix)
+        run_env.unset('PETSC_ARCH')
+
     def setup_dependent_environment(self, spack_env, run_env, dependent_spec):
-        # set up PETSC_DIR for everyone using PETSc package
+        # Set up PETSC_DIR for everyone using PETSc package
         spack_env.set('PETSC_DIR', self.prefix)
+        spack_env.unset('PETSC_ARCH')

{"golden_diff": "diff --git a/var/spack/repos/builtin/packages/petsc/package.py b/var/spack/repos/builtin/packages/petsc/package.py\n--- a/var/spack/repos/builtin/packages/petsc/package.py\n+++ b/var/spack/repos/builtin/packages/petsc/package.py\n@@ -218,6 +218,16 @@\n                         '-pc_type', 'hypre',\n                         '-pc_hypre_type', 'boomeramg')\n \n+    def setup_environment(self, spack_env, run_env):\n+        # configure fails if these env vars are set outside of Spack\n+        spack_env.unset('PETSC_DIR')\n+        spack_env.unset('PETSC_ARCH')\n+\n+        # Set PETSC_DIR in the module file\n+        run_env.set('PETSC_DIR', self.prefix)\n+        run_env.unset('PETSC_ARCH')\n+\n     def setup_dependent_environment(self, spack_env, run_env, dependent_spec):\n-        # set up PETSC_DIR for everyone using PETSc package\n+        # Set up PETSC_DIR for everyone using PETSc package\n         spack_env.set('PETSC_DIR', self.prefix)\n+        spack_env.unset('PETSC_ARCH')\n", "issue": "Unable to build PETSc, PETSC_DIR already set\n`configure` fails immediately if the `PETSC_DIR` environment variable is already set. Spack should unset this.\r\n```\r\n===============================================================================\r\n             Configuring PETSc to compile on your system                       \r\n===============================================================================\r\nTESTING: configureExternalPackagesDir from config.framework(/scratch/ajstewart/spack-stage/spack-stage-sFRYcv/petsc-3.7.4/config/BuildSystem/config/framework.py:834)\r\nTESTING: configureDebuggers from config.utilities.debuggers(/scratch/ajstewart/spack-stage/spack-stage-sFRYcv/petsc-3.7.4/config/BuildSystem/config/utilities/debuggers.py:22)\r\nTESTING: configureGit from config.sourceControl(/scratch/ajstewart/spack-stage/spack-stage-sFRYcv/petsc-3.7.4/config/BuildSystem/config/sourceControl.py:24)\r\nTESTING: configureMercurial from config.sourceControl(/scratch/ajstewart/spack-stage/spack-stage-sFRYcv/petsc-3.7.4/config/BuildSystem/config/sourceControl.py:35)\r\nTESTING: configureCLanguage from PETSc.options.languages(/scratch/ajstewart/spack-stage/spack-stage-sFRYcv/petsc-3.7.4/config/PETSc/options/languages.py:27)\r\nTESTING: configureDirectories from PETSc.options.petscdir(/scratch/ajstewart/spack-stage/spack-stage-sFRYcv/petsc-3.7.4/config/PETSc/options/petscdir.py:23)\r\n*******************************************************************************\r\n         UNABLE to CONFIGURE with GIVEN OPTIONS    (see configure.log for details):\r\n-------------------------------------------------------------------------------\r\nThe environmental variable PETSC_DIR /home/ajstewart/petsc MUST be the current directory /scratch/ajstewart/spack-stage/spack-stage-sFRYcv/petsc-3.7.4\r\n*******************************************************************************\r\n```\n", "before_files": [{"content": "##############################################################################\n# Copyright (c) 2013-2016, Lawrence Livermore National Security, LLC.\n# Produced at the Lawrence Livermore National Laboratory.\n#\n# This file is part of Spack.\n# Created by Todd Gamblin, tgamblin@llnl.gov, All rights reserved.\n# LLNL-CODE-647188\n#\n# For details, see https://github.com/llnl/spack\n# Please also see the LICENSE file for our notice and the LGPL.\n#\n# This program is free software; you can redistribute it and/or modify\n# it under the terms of the GNU Lesser General Public License (as\n# published by the Free Software Foundation) version 2.1, February 1999.\n#\n# This program is distributed in the hope that it will be useful, but\n# WITHOUT ANY WARRANTY; without even the IMPLIED WARRANTY OF\n# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the terms and\n# conditions of the GNU Lesser General Public License for more details.\n#\n# You should have received a copy of the GNU Lesser General Public\n# License along with this program; if not, write to the Free Software\n# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA\n##############################################################################\n\nimport os\nimport sys\nfrom spack import *\n\n\nclass Petsc(Package):\n    \"\"\"PETSc is a suite of data structures and routines for the scalable\n    (parallel) solution of scientific applications modeled by partial\n    differential equations.\n    \"\"\"\n\n    homepage = \"http://www.mcs.anl.gov/petsc/index.html\"\n    url = \"http://ftp.mcs.anl.gov/pub/petsc/release-snapshots/petsc-3.5.3.tar.gz\"\n\n    version('develop', git='https://bitbucket.org/petsc/petsc.git', tag='master')\n    version('for-pflotran-0.1.0', git='https://bitbucket.org/petsc/petsc.git',\n            commit='7943f4e1472fff9cf1fc630a1100136616e4970f')\n\n    version('3.7.4', 'aaf94fa54ef83022c14091f10866eedf')\n    version('3.7.2', '50da49867ce7a49e7a0c1b37f4ec7b34')\n    version('3.6.4', '7632da2375a3df35b8891c9526dbdde7')\n    version('3.6.3', '91dd3522de5a5ef039ff8f50800db606')\n    version('3.5.3', 'd4fd2734661e89f18ac6014b5dd1ef2f')\n    version('3.5.2', 'ad170802b3b058b5deb9cd1f968e7e13')\n    version('3.5.1', 'a557e029711ebf425544e117ffa44d8f')\n    version('3.4.4', '7edbc68aa6d8d6a3295dd5f6c2f6979d')\n\n    variant('shared',  default=True,\n            description='Enables the build of shared libraries')\n    variant('mpi',     default=True,  description='Activates MPI support')\n    variant('double',  default=True,\n            description='Switches between single and double precision')\n    variant('complex', default=False, description='Build with complex numbers')\n    variant('debug',   default=False, description='Compile in debug mode')\n\n    variant('metis',   default=True,\n            description='Activates support for metis and parmetis')\n    variant('hdf5',    default=True,\n            description='Activates support for HDF5 (only parallel)')\n    variant('boost',   default=True,  description='Activates support for Boost')\n    variant('hypre',   default=True,\n            description='Activates support for Hypre (only parallel)')\n    variant('mumps',   default=True,\n            description='Activates support for MUMPS (only parallel)')\n    variant('superlu-dist', default=True,\n            description='Activates support for SuperluDist (only parallel)')\n\n    # Virtual dependencies\n    # Git repository needs sowing to build Fortran interface\n    depends_on('sowing', when='@develop')\n\n    depends_on('blas')\n    depends_on('lapack')\n    depends_on('mpi', when='+mpi')\n\n    # Build dependencies\n    depends_on('python @2.6:2.7', type='build')\n\n    # Other dependencies\n    depends_on('boost', when='@:3.5+boost')\n    depends_on('metis@5:', when='+metis')\n\n    depends_on('hdf5+mpi', when='+hdf5+mpi')\n    depends_on('parmetis', when='+metis+mpi')\n    # Hypre does not support complex numbers.\n    # Also PETSc prefer to build it without internal superlu, likely due to\n    # conflict in headers see\n    # https://bitbucket.org/petsc/petsc/src/90564b43f6b05485163c147b464b5d6d28cde3ef/config/BuildSystem/config/packages/hypre.py\n    depends_on('hypre~internal-superlu', when='+hypre+mpi~complex')\n    depends_on('superlu-dist@:4.3', when='@3.4.4:3.6.4+superlu-dist+mpi')\n    depends_on('superlu-dist@5.0.0:', when='@3.7:+superlu-dist+mpi')\n    depends_on('superlu-dist@5.0.0:', when='@for-pflotran-0.1.0+superlu-dist+mpi')\n    depends_on('mumps+mpi', when='+mumps+mpi')\n    depends_on('scalapack', when='+mumps+mpi')\n\n    def mpi_dependent_options(self):\n        if '~mpi' in self.spec:\n            compiler_opts = [\n                '--with-cc=%s' % os.environ['CC'],\n                '--with-cxx=%s' % (os.environ['CXX']\n                                   if self.compiler.cxx is not None else '0'),\n                '--with-fc=%s' % (os.environ['FC']\n                                  if self.compiler.fc is not None else '0'),\n                '--with-mpi=0'\n            ]\n            error_message_fmt = \\\n                '\\t{library} support requires \"+mpi\" to be activated'\n\n            # If mpi is disabled (~mpi), it's an error to have any of these\n            # enabled. This generates a list of any such errors.\n            errors = [\n                error_message_fmt.format(library=x)\n                for x in ('hdf5', 'hypre', 'parmetis', 'mumps', 'superlu-dist')\n                if ('+' + x) in self.spec]\n            if errors:\n                errors = ['incompatible variants given'] + errors\n                raise RuntimeError('\\n'.join(errors))\n        else:\n            compiler_opts = [\n                '--with-mpi=1',\n                '--with-mpi-dir=%s' % self.spec['mpi'].prefix,\n            ]\n        if sys.platform != \"darwin\":\n            compiler_opts.extend([\n                '--with-cpp=cpp',\n                '--with-cxxcpp=cpp',\n            ])\n        return compiler_opts\n\n    def install(self, spec, prefix):\n        options = ['--with-ssl=0',\n                   '--with-x=0',\n                   '--download-c2html=0',\n                   '--download-sowing=0',\n                   '--download-hwloc=0']\n        options.extend(self.mpi_dependent_options())\n        options.extend([\n            '--with-precision=%s' % (\n                'double' if '+double' in spec else 'single'),\n            '--with-scalar-type=%s' % (\n                'complex' if '+complex' in spec else 'real'),\n            '--with-shared-libraries=%s' % ('1' if '+shared' in spec else '0'),\n            '--with-debugging=%s' % ('1' if '+debug' in spec else '0')\n        ])\n        # Make sure we use exactly the same Blas/Lapack libraries\n        # across the DAG. To that end list them explicitly\n        lapack_blas = spec['lapack'].lapack_libs + spec['blas'].blas_libs\n        options.extend([\n            '--with-blas-lapack-lib=%s' % lapack_blas.joined()\n        ])\n\n        # Activates library support if needed\n        for library in ('metis', 'boost', 'hdf5', 'hypre', 'parmetis',\n                        'mumps', 'scalapack'):\n            options.append(\n                '--with-{library}={value}'.format(\n                    library=library, value=('1' if library in spec else '0'))\n            )\n            if library in spec:\n                options.append(\n                    '--with-{library}-dir={path}'.format(\n                        library=library, path=spec[library].prefix)\n                )\n        # PETSc does not pick up SuperluDist from the dir as they look for\n        # superlu_dist_4.1.a\n        if 'superlu-dist' in spec:\n            options.extend([\n                '--with-superlu_dist-include=%s' %\n                spec['superlu-dist'].prefix.include,\n                '--with-superlu_dist-lib=%s' %\n                join_path(spec['superlu-dist'].prefix.lib,\n                          'libsuperlu_dist.a'),\n                '--with-superlu_dist=1'\n            ])\n        else:\n            options.append(\n                '--with-superlu_dist=0'\n            )\n\n        configure('--prefix=%s' % prefix, *options)\n\n        # PETSc has its own way of doing parallel make.\n        make('MAKE_NP=%s' % make_jobs, parallel=False)\n        make(\"install\")\n\n        # solve Poisson equation in 2D to make sure nothing is broken:\n        if ('mpi' in spec) and self.run_tests:\n            with working_dir('src/ksp/ksp/examples/tutorials'):\n                env['PETSC_DIR'] = self.prefix\n                cc = Executable(spec['mpi'].mpicc)\n                cc('ex50.c', '-I%s' % prefix.include, '-L%s' % prefix.lib,\n                   '-lpetsc', '-lm', '-o', 'ex50')\n                run = Executable(join_path(spec['mpi'].prefix.bin, 'mpirun'))\n                run('ex50', '-da_grid_x', '4', '-da_grid_y', '4')\n                if 'superlu-dist' in spec:\n                    run('ex50',\n                        '-da_grid_x', '4',\n                        '-da_grid_y', '4',\n                        '-pc_type', 'lu',\n                        '-pc_factor_mat_solver_package', 'superlu_dist')\n\n                if 'mumps' in spec:\n                    run('ex50',\n                        '-da_grid_x', '4',\n                        '-da_grid_y', '4',\n                        '-pc_type', 'lu',\n                        '-pc_factor_mat_solver_package', 'mumps')\n\n                if 'hypre' in spec:\n                    run('ex50',\n                        '-da_grid_x', '4',\n                        '-da_grid_y', '4',\n                        '-pc_type', 'hypre',\n                        '-pc_hypre_type', 'boomeramg')\n\n    def setup_dependent_environment(self, spack_env, run_env, dependent_spec):\n        # set up PETSC_DIR for everyone using PETSc package\n        spack_env.set('PETSC_DIR', self.prefix)\n", "path": "var/spack/repos/builtin/packages/petsc/package.py"}], "after_files": [{"content": "##############################################################################\n# Copyright (c) 2013-2016, Lawrence Livermore National Security, LLC.\n# Produced at the Lawrence Livermore National Laboratory.\n#\n# This file is part of Spack.\n# Created by Todd Gamblin, tgamblin@llnl.gov, All rights reserved.\n# LLNL-CODE-647188\n#\n# For details, see https://github.com/llnl/spack\n# Please also see the LICENSE file for our notice and the LGPL.\n#\n# This program is free software; you can redistribute it and/or modify\n# it under the terms of the GNU Lesser General Public License (as\n# published by the Free Software Foundation) version 2.1, February 1999.\n#\n# This program is distributed in the hope that it will be useful, but\n# WITHOUT ANY WARRANTY; without even the IMPLIED WARRANTY OF\n# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the terms and\n# conditions of the GNU Lesser General Public License for more details.\n#\n# You should have received a copy of the GNU Lesser General Public\n# License along with this program; if not, write to the Free Software\n# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA\n##############################################################################\n\nimport os\nimport sys\nfrom spack import *\n\n\nclass Petsc(Package):\n    \"\"\"PETSc is a suite of data structures and routines for the scalable\n    (parallel) solution of scientific applications modeled by partial\n    differential equations.\n    \"\"\"\n\n    homepage = \"http://www.mcs.anl.gov/petsc/index.html\"\n    url = \"http://ftp.mcs.anl.gov/pub/petsc/release-snapshots/petsc-3.5.3.tar.gz\"\n\n    version('develop', git='https://bitbucket.org/petsc/petsc.git', tag='master')\n    version('for-pflotran-0.1.0', git='https://bitbucket.org/petsc/petsc.git',\n            commit='7943f4e1472fff9cf1fc630a1100136616e4970f')\n\n    version('3.7.4', 'aaf94fa54ef83022c14091f10866eedf')\n    version('3.7.2', '50da49867ce7a49e7a0c1b37f4ec7b34')\n    version('3.6.4', '7632da2375a3df35b8891c9526dbdde7')\n    version('3.6.3', '91dd3522de5a5ef039ff8f50800db606')\n    version('3.5.3', 'd4fd2734661e89f18ac6014b5dd1ef2f')\n    version('3.5.2', 'ad170802b3b058b5deb9cd1f968e7e13')\n    version('3.5.1', 'a557e029711ebf425544e117ffa44d8f')\n    version('3.4.4', '7edbc68aa6d8d6a3295dd5f6c2f6979d')\n\n    variant('shared',  default=True,\n            description='Enables the build of shared libraries')\n    variant('mpi',     default=True,  description='Activates MPI support')\n    variant('double',  default=True,\n            description='Switches between single and double precision')\n    variant('complex', default=False, description='Build with complex numbers')\n    variant('debug',   default=False, description='Compile in debug mode')\n\n    variant('metis',   default=True,\n            description='Activates support for metis and parmetis')\n    variant('hdf5',    default=True,\n            description='Activates support for HDF5 (only parallel)')\n    variant('boost',   default=True,  description='Activates support for Boost')\n    variant('hypre',   default=True,\n            description='Activates support for Hypre (only parallel)')\n    variant('mumps',   default=True,\n            description='Activates support for MUMPS (only parallel)')\n    variant('superlu-dist', default=True,\n            description='Activates support for SuperluDist (only parallel)')\n\n    # Virtual dependencies\n    # Git repository needs sowing to build Fortran interface\n    depends_on('sowing', when='@develop')\n\n    depends_on('blas')\n    depends_on('lapack')\n    depends_on('mpi', when='+mpi')\n\n    # Build dependencies\n    depends_on('python @2.6:2.7', type='build')\n\n    # Other dependencies\n    depends_on('boost', when='@:3.5+boost')\n    depends_on('metis@5:', when='+metis')\n\n    depends_on('hdf5+mpi', when='+hdf5+mpi')\n    depends_on('parmetis', when='+metis+mpi')\n    # Hypre does not support complex numbers.\n    # Also PETSc prefer to build it without internal superlu, likely due to\n    # conflict in headers see\n    # https://bitbucket.org/petsc/petsc/src/90564b43f6b05485163c147b464b5d6d28cde3ef/config/BuildSystem/config/packages/hypre.py\n    depends_on('hypre~internal-superlu', when='+hypre+mpi~complex')\n    depends_on('superlu-dist@:4.3', when='@3.4.4:3.6.4+superlu-dist+mpi')\n    depends_on('superlu-dist@5.0.0:', when='@3.7:+superlu-dist+mpi')\n    depends_on('superlu-dist@5.0.0:', when='@for-pflotran-0.1.0+superlu-dist+mpi')\n    depends_on('mumps+mpi', when='+mumps+mpi')\n    depends_on('scalapack', when='+mumps+mpi')\n\n    def mpi_dependent_options(self):\n        if '~mpi' in self.spec:\n            compiler_opts = [\n                '--with-cc=%s' % os.environ['CC'],\n                '--with-cxx=%s' % (os.environ['CXX']\n                                   if self.compiler.cxx is not None else '0'),\n                '--with-fc=%s' % (os.environ['FC']\n                                  if self.compiler.fc is not None else '0'),\n                '--with-mpi=0'\n            ]\n            error_message_fmt = \\\n                '\\t{library} support requires \"+mpi\" to be activated'\n\n            # If mpi is disabled (~mpi), it's an error to have any of these\n            # enabled. This generates a list of any such errors.\n            errors = [\n                error_message_fmt.format(library=x)\n                for x in ('hdf5', 'hypre', 'parmetis', 'mumps', 'superlu-dist')\n                if ('+' + x) in self.spec]\n            if errors:\n                errors = ['incompatible variants given'] + errors\n                raise RuntimeError('\\n'.join(errors))\n        else:\n            compiler_opts = [\n                '--with-mpi=1',\n                '--with-mpi-dir=%s' % self.spec['mpi'].prefix,\n            ]\n        if sys.platform != \"darwin\":\n            compiler_opts.extend([\n                '--with-cpp=cpp',\n                '--with-cxxcpp=cpp',\n            ])\n        return compiler_opts\n\n    def install(self, spec, prefix):\n        options = ['--with-ssl=0',\n                   '--with-x=0',\n                   '--download-c2html=0',\n                   '--download-sowing=0',\n                   '--download-hwloc=0']\n        options.extend(self.mpi_dependent_options())\n        options.extend([\n            '--with-precision=%s' % (\n                'double' if '+double' in spec else 'single'),\n            '--with-scalar-type=%s' % (\n                'complex' if '+complex' in spec else 'real'),\n            '--with-shared-libraries=%s' % ('1' if '+shared' in spec else '0'),\n            '--with-debugging=%s' % ('1' if '+debug' in spec else '0')\n        ])\n        # Make sure we use exactly the same Blas/Lapack libraries\n        # across the DAG. To that end list them explicitly\n        lapack_blas = spec['lapack'].lapack_libs + spec['blas'].blas_libs\n        options.extend([\n            '--with-blas-lapack-lib=%s' % lapack_blas.joined()\n        ])\n\n        # Activates library support if needed\n        for library in ('metis', 'boost', 'hdf5', 'hypre', 'parmetis',\n                        'mumps', 'scalapack'):\n            options.append(\n                '--with-{library}={value}'.format(\n                    library=library, value=('1' if library in spec else '0'))\n            )\n            if library in spec:\n                options.append(\n                    '--with-{library}-dir={path}'.format(\n                        library=library, path=spec[library].prefix)\n                )\n        # PETSc does not pick up SuperluDist from the dir as they look for\n        # superlu_dist_4.1.a\n        if 'superlu-dist' in spec:\n            options.extend([\n                '--with-superlu_dist-include=%s' %\n                spec['superlu-dist'].prefix.include,\n                '--with-superlu_dist-lib=%s' %\n                join_path(spec['superlu-dist'].prefix.lib,\n                          'libsuperlu_dist.a'),\n                '--with-superlu_dist=1'\n            ])\n        else:\n            options.append(\n                '--with-superlu_dist=0'\n            )\n\n        configure('--prefix=%s' % prefix, *options)\n\n        # PETSc has its own way of doing parallel make.\n        make('MAKE_NP=%s' % make_jobs, parallel=False)\n        make(\"install\")\n\n        # solve Poisson equation in 2D to make sure nothing is broken:\n        if ('mpi' in spec) and self.run_tests:\n            with working_dir('src/ksp/ksp/examples/tutorials'):\n                env['PETSC_DIR'] = self.prefix\n                cc = Executable(spec['mpi'].mpicc)\n                cc('ex50.c', '-I%s' % prefix.include, '-L%s' % prefix.lib,\n                   '-lpetsc', '-lm', '-o', 'ex50')\n                run = Executable(join_path(spec['mpi'].prefix.bin, 'mpirun'))\n                run('ex50', '-da_grid_x', '4', '-da_grid_y', '4')\n                if 'superlu-dist' in spec:\n                    run('ex50',\n                        '-da_grid_x', '4',\n                        '-da_grid_y', '4',\n                        '-pc_type', 'lu',\n                        '-pc_factor_mat_solver_package', 'superlu_dist')\n\n                if 'mumps' in spec:\n                    run('ex50',\n                        '-da_grid_x', '4',\n                        '-da_grid_y', '4',\n                        '-pc_type', 'lu',\n                        '-pc_factor_mat_solver_package', 'mumps')\n\n                if 'hypre' in spec:\n                    run('ex50',\n                        '-da_grid_x', '4',\n                        '-da_grid_y', '4',\n                        '-pc_type', 'hypre',\n                        '-pc_hypre_type', 'boomeramg')\n\n    def setup_environment(self, spack_env, run_env):\n        # configure fails if these env vars are set outside of Spack\n        spack_env.unset('PETSC_DIR')\n        spack_env.unset('PETSC_ARCH')\n\n        # Set PETSC_DIR in the module file\n        run_env.set('PETSC_DIR', self.prefix)\n        run_env.unset('PETSC_ARCH')\n\n    def setup_dependent_environment(self, spack_env, run_env, dependent_spec):\n        # Set up PETSC_DIR for everyone using PETSc package\n        spack_env.set('PETSC_DIR', self.prefix)\n        spack_env.unset('PETSC_ARCH')\n", "path": "var/spack/repos/builtin/packages/petsc/package.py"}]}