Skip to content

Commit

Permalink
imports: move conditional to directives.py
Browse files Browse the repository at this point in the history
`conditional()`, which defines conditional variant values, and the other ways to declare
variant values should probably be in a layer above `spack.variant`. This does the simple
thing and moves *just* `conditional()` to `spack.directives` to avoid a circular import.

We can revisit the public variant interface later, when we split packages from core.

Co-authored-by: Harmen Stoppels <me@harmenstoppels.nl>
Signed-off-by: Todd Gamblin <tgamblin@llnl.gov>
  • Loading branch information
tgamblin and haampie committed Nov 11, 2024
1 parent a9e6074 commit 6961514
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 19 deletions.
10 changes: 10 additions & 0 deletions lib/spack/spack/directives.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ class OpenMpi(Package):
"DirectiveMeta",
"DisableRedistribute",
"version",
"conditional",
"conflicts",
"depends_on",
"extends",
Expand Down Expand Up @@ -577,6 +578,15 @@ def _execute_patch(pkg_or_dep: Union["spack.package_base.PackageBase", Dependenc
return _execute_patch


def conditional(*values: List[Any], when: Optional[WhenType] = None):
"""Conditional values that can be used in variant declarations."""
# _make_when_spec returns None when the condition is statically false.
when = _make_when_spec(when)
return spack.variant.ConditionalVariantValues(
spack.variant.Value(x, when=when) for x in values
)


@directive("variants")
def variant(
name: str,
Expand Down
7 changes: 1 addition & 6 deletions lib/spack/spack/package.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,12 +103,7 @@
from spack.spec import InvalidSpecDetected, Spec
from spack.util.executable import *
from spack.util.filesystem import file_command, fix_darwin_install_name, mime_type
from spack.variant import (
any_combination_of,
auto_or_any_combination_of,
conditional,
disjoint_sets,
)
from spack.variant import any_combination_of, auto_or_any_combination_of, disjoint_sets
from spack.version import Version, ver

# These are just here for editor support; they will be replaced when the build env
Expand Down
11 changes: 2 additions & 9 deletions lib/spack/spack/variant.py
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ def _flatten(values) -> Collection:

flattened: List = []
for item in values:
if isinstance(item, _ConditionalVariantValues):
if isinstance(item, ConditionalVariantValues):
flattened.extend(item)
else:
flattened.append(item)
Expand Down Expand Up @@ -884,17 +884,10 @@ def prevalidate_variant_value(
)


class _ConditionalVariantValues(lang.TypedMutableSequence):
class ConditionalVariantValues(lang.TypedMutableSequence):
"""A list, just with a different type"""


def conditional(*values: List[Any], when: Optional["spack.directives.WhenType"] = None):
"""Conditional values that can be used in variant declarations."""
# _make_when_spec returns None when the condition is statically false.
when = spack.directives._make_when_spec(when)
return _ConditionalVariantValues([Value(x, when=when) for x in values])


class DuplicateVariantError(error.SpecError):
"""Raised when the same variant occurs in a spec twice."""

Expand Down
4 changes: 2 additions & 2 deletions var/spack/repos/builtin/packages/geant4/package.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
# SPDX-License-Identifier: (Apache-2.0 OR MIT)

from spack.package import *
from spack.variant import _ConditionalVariantValues
from spack.variant import ConditionalVariantValues


class Geant4(CMakePackage):
Expand Down Expand Up @@ -180,7 +180,7 @@ class Geant4(CMakePackage):

def std_when(values):
for v in values:
if isinstance(v, _ConditionalVariantValues):
if isinstance(v, ConditionalVariantValues):
for c in v:
yield (c.value, c.when)
else:
Expand Down
4 changes: 2 additions & 2 deletions var/spack/repos/builtin/packages/vecgeom/package.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@


from spack.package import *
from spack.variant import _ConditionalVariantValues
from spack.variant import ConditionalVariantValues


class Vecgeom(CMakePackage, CudaPackage):
Expand Down Expand Up @@ -196,7 +196,7 @@ class Vecgeom(CMakePackage, CudaPackage):

def std_when(values):
for v in values:
if isinstance(v, _ConditionalVariantValues):
if isinstance(v, ConditionalVariantValues):
for c in v:
yield (c.value, c.when)
else:
Expand Down

0 comments on commit 6961514

Please sign in to comment.