Repository: spack/spack

Issue:
Add version of hypre using CMake
https://github.com/spack/spack/pull/24445#issuecomment-877487184

This will allow better integration with consumer codes that use CMake (see https://github.com/ECP-copa/Cabana/pull/426#issuecomment-872535952)

To Do
- [ ] make a virtual so that we can switch between `hypre` and `hypre-cmake`
- [ ] port consumers to the virtual


Discussion:
  @osborn9 @ulrikeyang can you review this PR?

  This PR modifies the following package(s), for which you are listed as a maintainer:

  * hypre-cmake

Can we do this in the `hypre` package instead? What's the oldest version of `hypre` that supports CMake? We can deprecate all versions that don't support CMake.

Resolving patch:
diff --git a/var/spack/repos/builtin/packages/cabana/package.py b/var/spack/repos/builtin/packages/cabana/package.py
--- a/var/spack/repos/builtin/packages/cabana/package.py
+++ b/var/spack/repos/builtin/packages/cabana/package.py
@@ -4,6 +4,7 @@
 # SPDX-License-Identifier: (Apache-2.0 OR MIT)
 
 from spack import *
+from spack.pkg.builtin.kokkos import Kokkos
 
 
 class Cabana(CMakePackage):
@@ -11,42 +12,70 @@ class Cabana(CMakePackage):
     """
     homepage = "https://github.com/ECP-copa/Cabana"
     git      = "https://github.com/ECP-copa/Cabana.git"
-    url      = "https://github.com/ECP-copa/Cabana/archive/0.1.0.tar.gz"
+    url      = "https://github.com/ECP-copa/Cabana/archive/0.4.0.tar.gz"
+
+    maintainers = ["junghans", "sslattery", "streeve"]
 
     version('master', branch='master')
+    version('0.4.0', sha256='c347d23dc4a5204f9cc5906ccf3454f0b0b1612351bbe0d1c58b14cddde81e85')
     version('0.3.0', sha256='fb67ab9aaf254b103ae0eb5cc913ddae3bf3cd0cf6010e9686e577a2981ca84f')
     version('0.2.0', sha256='3e0c0e224e90f4997f6c7e2b92f00ffa18f8bcff72f789e0908cea0828afc2cb')
     version('0.1.0', sha256='3280712facf6932b9d1aff375b24c932abb9f60a8addb0c0a1950afd0cb9b9cf')
     version('0.1.0-rc0', sha256='73754d38aaa0c2a1e012be6959787108fec142294774c23f70292f59c1bdc6c5')
 
-    variant('serial', default=True, description="enable Serial backend (default)")
-    variant('openmp', default=False, description="enable OpenMP backend")
-    variant('cuda', default=False, description="enable Cuda backend")
+    _kokkos_backends = Kokkos.devices_variants
+    for _backend in _kokkos_backends:
+        _deflt, _descr = _kokkos_backends[_backend]
+        variant(_backend.lower(), default=_deflt, description=_descr)
+
     variant('shared', default=True, description='Build shared libraries')
     variant('mpi', default=True, description='Build with mpi support')
+    variant('arborx', default=False, description='Build with ArborX support')
+    variant('heffte', default=False, description='Build with heFFTe support')
+    variant('hypre', default=False, description='Build with HYPRE support')
 
     depends_on("cmake@3.9:", type='build')
-    depends_on("kokkos-legacy+serial", when="@:0.2.0+serial")
-    depends_on("kokkos-legacy+openmp", when="@:0.2.0+openmp")
-    depends_on("kokkos-legacy+cuda", when="@:0.2.0+cuda")
-    depends_on("kokkos@3.1:+serial", when="@0.3.0:+serial")
-    depends_on("kokkos@3.1:+openmp", when="@0.3.0:+openmp")
-    depends_on("kokkos@3.1:+cuda", when="@0.3.0:+cuda")
+    _versions = {
+        ":0.2.0": "-legacy",
+        "0.3.0": "@3.1:",
+        "0.4.0": "@3.2:"
+    }
+    for _version in _versions:
+        _kk_version = _versions[_version]
+        for _backend in _kokkos_backends:
+            if (_kk_version == "-legacy" and _backend == 'pthread'):
+                _kk_spec = 'kokkos-legacy+pthreads'
+            elif (_kk_version == "-legacy" and
+                  _backend not in ['serial', 'openmp', 'cuda']):
+                continue
+            else:
+                _kk_spec = 'kokkos{0}+{1}'.format(_kk_version, _backend)
+            depends_on(_kk_spec, when='@{0}+{1}'.format(_version, _backend))
+    depends_on("arborx", when="@0.3.0:+arborx")
+    depends_on("heffte@2.0:", when="@0.4.0:+heffte")
+    depends_on("hypre-cmake@2.22.0:", when="@0.4.0:+hypre")
     depends_on('mpi', when='+mpi')
 
+    conflicts("+rocm", when="@:0.2.0")
+    conflicts("+sycl", when="@:0.3.0")
+
     def cmake_args(self):
         options = [
             '-DCabana_ENABLE_TESTING=ON',
-            '-DCabana_ENABLE_Serial=%s'  % (
-                'On' if '+serial'  in self.spec else 'Off'),
-            '-DCabana_ENABLE_OpenMP=%s'  % (
-                'On' if '+openmp'  in self.spec else 'Off'),
-            '-DCabana_ENABLE_Cuda=%s'  % (
-                'On' if '+cuda'  in self.spec else 'Off'),
-            '-DCabana_ENABLE_MPI=%s'  % (
-                'On' if '+mpi'  in self.spec else 'Off'),
             '-DBUILD_SHARED_LIBS=%s' % (
                 'On' if '+shared'  in self.spec else 'Off')
         ]
+        # These variables were removed in 0.3.0 (where backends are
+        # automatically used from Kokkos)
+        if self.spec.satisfies('@:0.2.0'):
+            backends = {'serial': 'Serial',
+                        'openmp': 'OpenMP',
+                        'cuda': 'Cuda'}
+            for backend in backends:
+                cbn_option = 'Cabana_ENABLE_' + backends[backend]
+                options.append(self.define_from_variant(cbn_option, backend))
+
+        if self.spec.satisfies('@:0.3.0'):
+            options.append(self.define_from_variant('Cabana_ENABLE_MPI', 'mpi'))
 
         return options
diff --git a/var/spack/repos/builtin/packages/hypre-cmake/package.py b/var/spack/repos/builtin/packages/hypre-cmake/package.py
new file mode 100644
--- /dev/null
+++ b/var/spack/repos/builtin/packages/hypre-cmake/package.py
@@ -0,0 +1,136 @@
+# Copyright 2013-2021 Lawrence Livermore National Security, LLC and other
+# Spack Project Developers. See the top-level COPYRIGHT file for details.
+#
+# SPDX-License-Identifier: (Apache-2.0 OR MIT)
+
+import sys
+
+from spack import *
+
+
+class HypreCmake(CMakePackage, CudaPackage):
+    """Hypre is a library of high performance preconditioners that
+       features parallel multigrid methods for both structured and
+       unstructured grid problems."""
+
+    homepage = "http://computing.llnl.gov/project/linear_solvers/software.php"
+    url      = "https://github.com/hypre-space/hypre/archive/v2.14.0.tar.gz"
+    git      = "https://github.com/hypre-space/hypre.git"
+
+    maintainers = ['ulrikeyang', 'osborn9', 'balay']
+
+    test_requires_compiler = True
+
+    version('develop', branch='master')
+    version('2.22.0', sha256='2c786eb5d3e722d8d7b40254f138bef4565b2d4724041e56a8fa073bda5cfbb5')
+
+    variant('shared', default=(sys.platform != 'darwin'),
+            description="Build shared library (disables static library)")
+    variant('superlu_dist', default=False,
+            description='Activates support for SuperLU_Dist library')
+    variant('int64', default=False,
+            description="Use 64bit integers")
+    variant('mixedint', default=False,
+            description="Use 64bit integers while reducing memory use")
+    variant('complex', default=False, description='Use complex values')
+    variant('mpi', default=True, description='Enable MPI support')
+    variant('openmp', default=False, description='Enable OpenMP support')
+    variant('debug', default=False,
+            description='Build debug instead of optimized version')
+    variant('unified_memory', default=False, description='Use unified memory')
+
+    depends_on("mpi", when='+mpi')
+    depends_on("blas")
+    depends_on("lapack")
+    depends_on('superlu-dist', when='+superlu_dist+mpi')
+
+    conflicts('+cuda', when='+int64')
+    conflicts('+unified_memory', when='~cuda')
+
+    def url_for_version(self, version):
+        if version >= Version('2.12.0'):
+            url = 'https://github.com/hypre-space/hypre/archive/v{0}.tar.gz'
+        else:
+            url = 'http://computing.llnl.gov/project/linear_solvers/download/hypre-{0}.tar.gz'
+
+        return url.format(version)
+
+    root_cmakelists_dir = 'src'
+
+    def cmake_args(self):
+        from_variant = self.define_from_variant
+        args = [
+            from_variant('HYPRE_WITH_MPI', 'mpi'),
+            from_variant('HYPRE_WITH_OPENMP', 'openmp'),
+            from_variant('HYPRE_WITH_BIGINT', 'int64'),
+            from_variant('HYPRE_WITH_MIXEDINT', 'mixedint'),
+            from_variant('HYPRE_WITH_COMPLEX', 'complex'),
+            from_variant('BUILD_SHARED_LIBS', 'shared'),
+            from_variant('HYPRE_WITH_DSUPERLU', 'superlu_dist'),
+            from_variant('HYPRE_WITH_CUDA', 'cuda'),
+            from_variant('HYPRE_ENABLE_UNIFIED_MEMORY', 'unified_memory'),
+        ]
+
+        return args
+
+    def setup_build_environment(self, env):
+        if '+cuda' in self.spec:
+            env.set('CUDA_HOME', self.spec['cuda'].prefix)
+            env.set('CUDA_PATH', self.spec['cuda'].prefix)
+            cuda_arch = self.spec.variants['cuda_arch'].value
+            if cuda_arch:
+                arch_sorted = list(sorted(cuda_arch, reverse=True))
+                env.set('HYPRE_CUDA_SM', arch_sorted[0])
+            # In CUDA builds hypre currently doesn't handle flags correctly
+            env.append_flags(
+                'CXXFLAGS', '-O2' if '~debug' in self.spec else '-g')
+
+    extra_install_tests = join_path('src', 'examples')
+
+    @run_after('install')
+    def cache_test_sources(self):
+        self.cache_extra_test_sources(self.extra_install_tests)
+
+    @property
+    def _cached_tests_work_dir(self):
+        """The working directory for cached test sources."""
+        return join_path(self.test_suite.current_test_cache_dir,
+                         self.extra_install_tests)
+
+    def test(self):
+        """Perform smoke test on installed HYPRE package."""
+        if '+mpi' not in self.spec:
+            print('Skipping: HYPRE must be installed with +mpi to run tests')
+            return
+
+        # Build copied and cached test examples
+        self.run_test('make',
+                      ['HYPRE_DIR={0}'.format(self.prefix), 'bigint'],
+                      purpose='test: building selected examples',
+                      work_dir=self._cached_tests_work_dir)
+
+        # Run the examples built above
+        for exe in ['./ex5big', './ex15big']:
+            self.run_test(exe, [], [], installed=False,
+                          purpose='test: ensuring {0} runs'.format(exe),
+                          skip_missing=True,
+                          work_dir=self._cached_tests_work_dir)
+
+    @property
+    def headers(self):
+        """Export the main hypre header, HYPRE.h; all other headers can be found
+        in the same directory.
+        Sample usage: spec['hypre'].headers.cpp_flags
+        """
+        hdrs = find_headers('HYPRE', self.prefix.include, recursive=False)
+        return hdrs or None
+
+    @property
+    def libs(self):
+        """Export the hypre library.
+        Sample usage: spec['hypre'].libs.ld_flags
+        """
+        is_shared = '+shared' in self.spec
+        libs = find_libraries('libHYPRE', root=self.prefix, shared=is_shared,
+                              recursive=True)
+        return libs or None


Test patch: