# 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)

from spack import *


class Tioga(CMakePackage):
    """Topology Independent Overset Grid Assembly (TIOGA)"""

    homepage = "https://github.com/jsitaraman/tioga"
    git      = "https://github.com/jsitaraman/tioga.git"

    version('master', branch='master')

    variant('shared', default=True,
            description="Enable building shared libraries")
    variant('pic', default=True,
            description="Position independent code")

    depends_on('mpi')

    # Tioga has the fortran module file problem with parallel builds
    parallel = False

    def cmake_args(self):
        spec = self.spec

        options = [
            '-DBUILD_SHARED_LIBS:BOOL=%s' % (
                'ON' if '+shared' in spec else 'OFF'),
            '-DCMAKE_POSITION_INDEPENDENT_CODE:BOOL=%s' % (
                'ON' if '+pic' in spec else 'OFF'),
            '-DCMAKE_C_COMPILER=%s' % spec['mpi'].mpicc,
            '-DCMAKE_CXX_COMPILER=%s' % spec['mpi'].mpicxx,
            '-DCMAKE_Fortran_COMPILER=%s' % spec['mpi'].mpifc,
            '-DMPI_CXX_COMPILER:PATH=%s' % spec['mpi'].mpicxx,
            '-DMPI_C_COMPILER:PATH=%s' % spec['mpi'].mpicc,
            '-DMPI_Fortran_COMPILER:PATH=%s' % spec['mpi'].mpifc
        ]

        if 'darwin' in spec.architecture:
            options.append('-DCMAKE_MACOSX_RPATH:BOOL=ON')

        return options

@@ -190,16 +190,26 @@
 OR MIT)%0A
+import sys
 %0Afrom sp
@@ -247,16 +247,29 @@
 ePackage
+, CudaPackage
 ):%0A    %22
@@ -436,16 +436,102 @@
 a.git%22%0A%0A
+    maintainers = %5B'jsitaraman', 'sayerhs'%5D%0A%0A    version('develop', branch='exawind')%0A
     vers
@@ -584,36 +584,56 @@
 hared', default=
-True
+sys.platform != 'darwin'
 ,%0A            de
@@ -647,23 +647,13 @@
 on=%22
-Enable b
+B
 uild
-ing
  sha
@@ -758,28 +758,472 @@
 e%22)%0A
-%0A    depends_on('mpi
+    variant('nodegid', default=True,%0A            description=%22Enable support for global Node IDs%22)%0A    variant('timers', default=False,%0A            description=%22Enable timers%22)%0A    variant('stats', default=False,%0A            description=%22Enable output of holecut stats%22)%0A    variant('cxxstd', default='11',%0A            values=('11', '14'), multi=False,%0A            description=%22C++ standard to use%22)%0A%0A    depends_on('mpi')%0A    depends_on('cuda@9.0.0:', when='+cuda
 ')%0A%0A
@@ -1373,22 +1373,19 @@
         
-option
+arg
 s = %5B%0A  
@@ -1398,586 +1398,927 @@
     
-'-DBUILD_SHARED_LIBS:BOOL=%25s' %25 (%0A                'ON' if '+shared' in spec else 'OFF'),%0A            '-DCMAKE_POSITION_INDEPENDENT_CODE:BOOL=%25s' %25 (%0A                'ON' if '+pic' in spec else 'OFF'),%0A            '-DCMAKE_C_COMPILER=%25s' %25 spec%5B'mpi'%5D.mpicc,%0A            '-DCMAKE_CXX_COMPILER=%25s' %25 spec%5B'mpi'%5D.mpicxx,%0A            '-DCMAKE_Fortran_COMPILER=%25s' %25 spec%5B'mpi'%5D.mpifc,%0A            '-DMPI_CXX_COMPILER:PATH=%25s' %25 spec%5B'mpi'%5D.mpicxx,%0A            '-DMPI_C_COMPILER:PATH=%25s' %25 spec%5B'mpi'%5D.mpicc,%0A            '-DMPI_Fortran_COMPILER:PATH=%25s' %25 spec%5B'mpi'%5D.mpifc%0A        %5D
+self.define_from_variant('BUILD_SHARED_LIBS', 'shared'),%0A            self.define_from_variant('CMAKE_POSITION_INDEPENDENT_CODE', 'pic'),%0A            self.define_from_variant('CMAKE_CXX_STANDARD', 'cxxstd'),%0A            self.define_from_variant('TIOGA_HAS_NODEGID', 'nodegid'),%0A            self.define_from_variant('TIOGA_ENABLE_TIMERS', 'timers'),%0A            self.define_from_variant('TIOGA_OUTPUT_STATS', 'stats'),%0A            self.define_from_variant('TIOGA_ENABLE_CUDA', 'cuda'),%0A        %5D%0A%0A        if '+cuda' in self.spec:%0A            args.append(self.define('CMAKE_CUDA_SEPARABLE_COMPILATION', True))%0A%0A            # Currently TIOGA only supports one device arch during specialization%0A            cuda_arch = self.spec.variants%5B'cuda_arch'%5D.value%0A            if cuda_arch:%0A                arch_sorted = list(sorted(cuda_arch, reverse=True))%0A                args.append(self.define('TIOGA_CUDA_SM', arch_sorted%5B0%5D))
 %0A%0A  
@@ -2369,22 +2369,19 @@
         
-option
+arg
 s.append
@@ -2385,11 +2385,21 @@
 end(
-'-D
+self.define('
 CMAK
@@ -2416,17 +2416,16 @@
 PATH
-:BOOL=ON'
+', True)
 )%0A%0A 
@@ -2442,12 +2442,9 @@
 urn 
-option
+arg
 s%0A

add variants: +cuda and more (#21884)