Skip to content

Commit

Permalink
adding built_types tests, and function class_to_mod
Browse files Browse the repository at this point in the history
Signed-off-by: vsoch <vsoch@users.noreply.github.com>
  • Loading branch information
vsoch committed Apr 30, 2021
1 parent 72ca58a commit 1b39415
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 9 deletions.
15 changes: 9 additions & 6 deletions lib/spack/spack/build_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,14 @@
"""Build types can add different flags depending on the compiler.
"""

from spack.util.naming import class_to_mod


class BuildTypeBase(object):
"""A build type base provides base functions to look up flags for a compiler.
"""
default = None
compiler_attrs = []
cuda_attrs = []

def __init__(self, spec):
self.spec = spec
Expand All @@ -20,7 +23,7 @@ def __init__(self, spec):
def name(self):
"""get the name for the class
"""
return self.__class__.__name__
return class_to_mod(self.__class__.__name__)

def get_compiler_flags(self, group):
"""
Expand Down Expand Up @@ -73,7 +76,7 @@ class RelWithDeb(BuildTypeBase):
cuda_attrs = []


class BasicDebug(BuildTypeBase):
class Debug(BuildTypeBase):
"""
The debug build type corresponds with the user asking for
spack_build_type=debug
Expand All @@ -82,7 +85,7 @@ class BasicDebug(BuildTypeBase):
cuda_attrs = ['debug_flag']


class DebugOptimized(BuildTypeBase):
class DebugOpt(BuildTypeBase):
compiler_attrs = ['debug_flag', 'debug_optimize_flag']
cuda_attrs = ['debug_flag']

Expand All @@ -93,8 +96,8 @@ class DebugMax(BuildTypeBase):


debug_types = {"debug", "debug_opt", "debug_max"}
build_types = {'debug': BasicDebug, 'debug_opt': DebugOptimized,
'debug_max': DebugMax, "rel_with_deb": RelWithDeb}
build_types = {'debug': Debug, 'debug_opt': DebugOpt, 'debug_max': DebugMax,
"rel_with_deb": RelWithDeb}


def get_build_type(spec):
Expand Down
2 changes: 0 additions & 2 deletions lib/spack/spack/package.py
Original file line number Diff line number Diff line change
Expand Up @@ -2668,8 +2668,6 @@ class Package(PackageBase):
run_after('install')(PackageBase.sanity_check_prefix)

spack.directives.variant('spack_built_type', default='rel_with_deb',
help="Set to control compiler flags for optimization \
and debug levels",
values=spack.build_types.build_types.values())


Expand Down
33 changes: 33 additions & 0 deletions lib/spack/spack/test/build_types.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# 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 inspect

import spack.build_types
from spack.util.naming import class_to_mod


def test_class_to_mod():
"""
Ensure that every module in build_types has a key via class_to_mod.
"""
classes = inspect.getmembers(spack.build_types, inspect.isclass)
for classname, _ in classes:

# This is more of an abstract class
if classname == "BuildTypeBase":
continue

build_type = class_to_mod(classname)
assert build_type in spack.build_types.build_types


def test_build_type_flags():
"""
Every build type is required to have a known set of flag attributes.
"""
for build_type in spack.build_types.build_types.values():
assert hasattr(build_type, "compiler_attrs")
assert hasattr(build_type, "cuda_attrs")
2 changes: 1 addition & 1 deletion lib/spack/spack/util/classes.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import inspect

__all__ = [
'list_classes'
'list_classes',
]


Expand Down
12 changes: 12 additions & 0 deletions lib/spack/spack/util/naming.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

__all__ = [
'mod_to_class',
'class_to_mod',
'spack_module_to_python_module',
'valid_module_name',
'valid_fully_qualified_module_name',
Expand Down Expand Up @@ -63,6 +64,17 @@ def mod_to_class(mod_name):
return class_name


def class_to_mod(class_name):
"""
Convert a class name to a module name.
The same conventions are used as in mod_to_class.
"""
# Replace all uppercase characters with _<lowercase>
replacer = lambda pattern: "_%s" % pattern.group(1).lower()
return re.sub('([A-Z])', replacer, class_name).strip('_')


def spack_module_to_python_module(mod_name):
"""Given a Spack module name, returns the name by which it can be
imported in Python.
Expand Down

0 comments on commit 1b39415

Please sign in to comment.