spack.solver package


spack.solver.asp module

class spack.solver.asp.CompilerParser(configuration)[source]

Bases: object

Parses configuration files, and builds a list of possible compilers for the solve.

add_compiler_from_concrete_spec(spec: Spec) None[source]

Account for compilers that are coming from concrete specs, through reuse.


spec – concrete spec to be reused

possible_compilers() List[KnownCompiler][source]
with_input_specs(input_specs: List[Spec]) CompilerParser[source]

Accounts for input specs when building the list of possible compilers.


input_specs – specs to be concretized

class spack.solver.asp.ConcreteSpecsByHash[source]

Bases: Mapping

Mapping containing concrete specs keyed by DAG hash.

The mapping is ensured to be consistent, i.e. if a spec in the mapping has a dependency with hash X, it is ensured to be the same object in memory as the spec keyed by X.

add(spec: Spec) bool[source]

Adds a new concrete spec to the mapping. Returns True if the spec was just added, False if the spec was already in the mapping.

Calling this function marks the spec as added explicitly.


spec – spec to be added


ValueError – if the spec is not concrete

explicit_items() Iterator[Tuple[str, Spec]][source]

Iterate on items that have been added explicitly, and not just as a dependency of other nodes.

spack.solver.asp.DEFAULT_OUTPUT_CONFIGURATION = (False, False, None, False)

Default output configuration for a solve

class spack.solver.asp.DeclaredVersion(version: str, idx: int, origin: Provenance)[source]

Bases: NamedTuple

Data class to contain information on declared versions used in the solve

idx: int

Unique index assigned to this version

origin: Provenance

Provenance of the version

version: str

String representation of the version

class spack.solver.asp.ErrorHandler(model)[source]

Bases: object

get_cause_tree(cause: Tuple[str, str]) List[str][source]

Get the cause tree associated with the given cause.


cause – The root cause of the tree (final condition)


A list of strings describing the causes, formatted to display tree structure.

handle_error(msg, *args)[source]

Handle an error state derived by the solver.

message(errors) str[source]
multiple_values_error(attribute, pkg)[source]
no_value_error(attribute, pkg)[source]
exception spack.solver.asp.InternalConcretizerError(msg)[source]

Bases: UnsatisfiableSpecError

Errors that indicate a bug in Spack.

class spack.solver.asp.KnownCompiler(spec: spack.spec.Spec, os: str, target: str, available: bool, compiler_obj: spack.compiler.Compiler | None)[source]

Bases: NamedTuple

Data class to collect information on compilers

available: bool

Alias for field number 3

compiler_obj: Compiler | None

Alias for field number 4

os: str

Alias for field number 1

spec: Spec

Alias for field number 0

target: str

Alias for field number 2

class spack.solver.asp.OutputConfiguration(timers, stats, out, setup_only)

Bases: tuple

Data class that contain configuration on what a clingo solve should output.

  • timers (bool) – Print out coarse timers for different solve phases.

  • stats (bool) – Whether to output Clingo’s internal solver statistics.

  • out – Optional output stream for the generated ASP program.

  • setup_only (bool) – if True, stop after setup and don’t solve (default False).


Alias for field number 2


Alias for field number 3


Alias for field number 1


Alias for field number 0

class spack.solver.asp.ProblemInstanceBuilder[source]

Bases: object

Provides an interface to construct a problem instance.

Once all the facts and rules have been added, the problem instance can be retrieved with:

>>> builder = ProblemInstanceBuilder()
>>> ...
>>> problem_instance = builder.value()

The problem instance can be added directly to the “control” structure of clingo.

append(rule: str) None[source]
fact(atom: AspFunction) None[source]
h1(header: str) None[source]
h2(header: str) None[source]
title(header: str, char: str) None[source]
value() str[source]
class spack.solver.asp.Provenance(value, names=None, *, module=None, qualname=None, type=None, start=1, boundary=None)[source]

Bases: IntEnum

Enumeration of the possible provenances of a version.

SPEC = 1
class spack.solver.asp.PyclingoDriver(cores=True)[source]

Bases: object

solve(setup, specs, reuse=None, output=None, control=None, allow_deprecated=False)[source]

Set up the input and solve for dependencies of specs.

  • setup (SpackSolverSetup) – An object to set up the ASP problem.

  • specs (list) – List of Spec objects to solve for.

  • reuse (None or list) – list of concrete specs that can be reused

  • output (None or OutputConfiguration) – configuration object to set the output of this solve.

  • control (clingo.Control) – configuration for the solver. If None, default values will be used

  • allow_deprecated – if True, allow deprecated versions in the solve


A tuple of the solve result, the timer for the different phases of the solve, and the internal statistics from clingo.

class spack.solver.asp.RequirementKind(value, names=None, *, module=None, qualname=None, type=None, start=1, boundary=None)[source]

Bases: Enum

Purpose / provenance of a requirement


Default requirement expressed under the ‘all’ attribute of packages.yaml


Requirement expressed on a specific package


Requirement expressed on a virtual package

class spack.solver.asp.RequirementParser(configuration)[source]

Bases: object

Parses requirements from files and configuration, and returns rules.

reject_requirement_constraint(pkg_name: str, *, constraint: Spec, kind: RequirementKind) bool[source]

Returns True if a requirement constraint should be rejected

rules(pkg: PackageBase) List[RequirementRule][source]
rules_from_conflict(pkg: PackageBase) List[RequirementRule][source]
rules_from_package_py(pkg) List[RequirementRule][source]
rules_from_prefer(pkg: PackageBase) List[RequirementRule][source]
rules_from_require(pkg: PackageBase) List[RequirementRule][source]
rules_from_virtual(virtual_str: str) List[RequirementRule][source]
class spack.solver.asp.RequirementRule(pkg_name: str, policy: str, requirements: List[spack.spec.Spec], condition: spack.spec.Spec, kind: RequirementKind, message: str | None)[source]

Bases: NamedTuple

Data class to collect information on a requirement

condition: Spec

Alias for field number 3

kind: RequirementKind

Alias for field number 4

message: str | None

Alias for field number 5

pkg_name: str

Alias for field number 0

policy: str

Alias for field number 1

requirements: List[Spec]

Alias for field number 2

class spack.solver.asp.Result(specs, asp=None)[source]

Bases: object

Result of an ASP solve.


Format an unsatisfiable core for human readability

Returns a list of strings, where each string is the human readable representation of a single fact in the core, including a newline.

Modeled after traceback.format_stack.


List of facts for each core

Separate cores are separated by an empty line Cores are not minimized


List of facts for each core

Separate cores are separated by an empty line

static format_unsolved(unsolved_specs)[source]

Create a message providing info on unsolved user specs and for each one show the associated candidate spec from the solver (if there is one).


Return a list of subset-minimal unsatisfiable cores.


Return a subset-minimal subset of the core.

Clingo cores may be thousands of lines when two facts are sufficient to ensure unsatisfiability. This algorithm reduces the core to only those essential facts.


Raise an appropriate error if the result is unsatisfiable.

The error is an SolverError, and includes the minimized cores resulting from the solve, formatted to be human readable.

property specs

List of concretized specs satisfying the initial abstract request.

property specs_by_input
property unsolved_specs

List of tuples pairing abstract input specs that were not solved with their associated candidate spec from the solver (if the solve completed).

class spack.solver.asp.ReusableSpecsSelector(configuration: Configuration)[source]

Bases: object

Selects specs that can be reused during concretization.

reusable_specs(specs: List[Spec]) List[Spec][source]
class spack.solver.asp.ReuseStrategy(value, names=None, *, module=None, qualname=None, type=None, start=1, boundary=None)[source]

Bases: Enum

NONE = 3
class spack.solver.asp.RuntimePropertyRecorder(setup)[source]

Bases: object

An object of this class is injected in callbacks to compilers, to let them declare properties of the runtimes they support and of the runtimes they provide, and to add runtime dependencies to the nodes using said compiler.

The usage of the object is the following. First, a runtime package name or the wildcard “*” are passed as an argument to __call__, to set which kind of package we are referring to. Then we can call one method with a directive-like API.


>>> pkg = RuntimePropertyRecorder(setup)
>>> # Every package compiled with %gcc has a link dependency on 'gcc-runtime'
>>> pkg("*").depends_on(
...     "gcc-runtime",
...     when="%gcc",
...     type="link",
...     description="If any package uses %gcc, it depends on gcc-runtime"
... )
>>> # The version of gcc-runtime is the same as the %gcc used to "compile" it
>>> pkg("gcc-runtime").requires("@=9.4.0", when="%gcc@=9.4.0")

Consume the facts collected by this object, and emits rules and facts for the runtimes.

depends_on(dependency_str: str, *, when: str, type: str, description: str, languages: List[str] | None = None) None[source]

Injects conditional dependencies on packages.

Conditional dependencies can be either “real” packages or virtual dependencies.

  • dependency_str – the dependency spec to inject

  • when – anonymous condition to be met on a package to have the dependency

  • type – dependency type

  • languages – languages needed by the package for the dependency to be considered

  • description – human-readable description of the rule for adding the dependency

requires(impose: str, *, when: str)[source]

Injects conditional requirements on a given package.

  • impose – constraint to be imposed

  • when – condition triggering the constraint


Resets the current state.

class spack.solver.asp.Solver[source]

Bases: object

This is the main external interface class for solving.

It manages solver configuration and preferences in one place. It sets up the solve and passes the setup method to the driver, as well.

solve(specs, out=None, timers=False, stats=False, tests=False, setup_only=False, allow_deprecated=False)[source]
  • specs (list) – List of Spec objects to solve for.

  • out – Optionally write the generate ASP program to a file-like object.

  • timers (bool) – Print out coarse timers for different solve phases.

  • stats (bool) – Print out detailed stats from clingo.

  • tests (bool or tuple) – If True, concretize test dependencies for all packages. If a tuple of package names, concretize test dependencies for named packages (defaults to False: do not concretize test dependencies).

  • setup_only (bool) – if True, stop after setup and don’t solve (default False).

  • allow_deprecated (bool) – allow deprecated version in the solve

solve_in_rounds(specs, out=None, timers=False, stats=False, tests=False, allow_deprecated=False)[source]

Solve for a stable model of specs in multiple rounds.

This relaxes the assumption of solve that everything must be consistent and solvable in a single round. Each round tries to maximize the reuse of specs from previous rounds.

The function is a generator that yields the result of each round.

  • specs (list) – list of Specs to solve.

  • out – Optionally write the generate ASP program to a file-like object.

  • timers (bool) – print timing if set to True

  • stats (bool) – print internal statistics if set to True

  • tests (bool) – add test dependencies to the solve

  • allow_deprecated (bool) – allow deprecated version in the solve

exception spack.solver.asp.SolverError(provided, conflicts)[source]

Bases: InternalConcretizerError

For cases where the solver is unable to produce a solution.

Such cases are unexpected because we allow for solutions with errors, so for example user specs that are over-constrained should still get a solution.

class spack.solver.asp.SpackSolverSetup(tests: bool = False)[source]

Bases: object

Class to set up and run a Spack concretization solve.


Define versions for constraints on virtuals.

Must be called before define_version_constraints().


Facts about available compilers.


Emit facts for reusable specs

condition(required_spec: ~spack.spec.Spec, imposed_spec: ~spack.spec.Spec | None = None, name: str | None = None, msg: str | None = None, transform_required: ~typing.Callable[[~spack.spec.Spec, ~typing.List[~spack.solver.core.AspFunction]], ~typing.List[~spack.solver.core.AspFunction]] | None = None, transform_imposed: ~typing.Callable[[~spack.spec.Spec, ~typing.List[~spack.solver.core.AspFunction]], ~typing.List[~spack.solver.core.AspFunction]] | None = <function remove_node>)[source]

Generate facts for a dependency or virtual provider condition.

  • required_spec – the constraints that triggers this condition

  • imposed_spec – the constraints that are imposed when this condition is triggered

  • name – name for required_spec (required if required_spec is anonymous, ignored if not)

  • msg – description of the condition

  • transform_required – transformation applied to facts from the required spec. Defaults to leave facts as they are.

  • transform_imposed – transformation applied to facts from the imposed spec. Defaults to removing “node” and “virtual_node” facts.


id of the condition created by this function

Return type:



Facts about compatible os’s specified in configs

define_ad_hoc_versions_from_specs(specs, origin, *, allow_deprecated: bool, require_checksum: bool)[source]

Add concrete versions to possible versions from lists of CLI/dev specs.

define_concrete_input_specs(specs, possible)[source]
define_package_versions_and_validate_preferences(possible_pkgs: Set[str], *, require_checksum: bool, allow_deprecated: bool)[source]

Declare any versions in specs not declared in packages.


Define the constraints to be imposed on the runtimes


Validate variant values from the command line.

Also add valid variant values from the command line to the possible values for a variant.


Define what version_satisfies(…) means in ASP logic.


Flushes all the effect rules collected so far, and clears the cache.

emit_facts_from_requirement_rules(rules: List[RequirementRule])[source]

Generate facts to enforce requirements.


rules – rules for which we want facts to be emitted


Facts on external packages, from packages.yaml and implicit externals.

impose(condition_id, imposed_spec, node=True, name=None, body=False)[source]

Translate ‘depends_on’ directives into ASP logic.

pkg_class(pkg_name: str) Type[PackageBase][source]
pkg_rules(pkg, tests)[source]

Output declared versions of a package.

This uses self.declared_versions so that we include any versions that arise from a spec.


Facts on concretization preferences, as read from packages.yaml

register_concrete_spec(spec, possible)[source]
setup(specs: List[Spec], *, reuse: List[Spec] | None = None, allow_deprecated: bool = False) str[source]

Generate an ASP program with relevant constraints for specs.

This calls methods on the solve driver to set up the problem with facts and rules from all possible dependencies of the input specs, as well as constraints from the specs themselves.

  • specs – list of Specs to solve

  • reuse – list of concrete specs that can be reused

  • allow_deprecated – if True adds deprecated versions into the solve

spec_clauses(spec: Spec, *, body: bool = False, transitive: bool = True, expand_hashes: bool = False, concrete_build_deps=False, required_from: str | None = None) List[AspFunction][source]

Wrap a call to _spec_clauses() into a try/except block with better error handling.

Arguments are as for _spec_clauses() except required_from.


required_from – name of package that caused this call.


Return list of clauses expressing spec’s version constraints.


Add facts about targets and target compatibility.

target_ranges(spec, single_target_fn)[source]

Flushes all the trigger rules collected so far, and clears the cache.

validate_and_define_versions_from_requirements(*, allow_deprecated: bool, require_checksum: bool)[source]

If package requirements mention concrete versions that are not mentioned elsewhere, then we need to collect those to mark them as possible versions. If they are abstract and statically have no match, then we need to throw an error. This function assumes all possible versions are already registered in self.possible_versions.

virtual_preferences(pkg_name, func)[source]

Call func(vspec, provider, i) for each of pkg’s provider prefs.

class spack.solver.asp.SpecBuilder(specs, hash_lookup=None)[source]

Bases: object

Class with actions to rebuild a spec from ASP results.

depends_on(parent_node, dependency_node, type)[source]
deprecated(node: NodeArgument, version: str) None[source]
external_spec_selected(node, idx)[source]

This means that the external spec and index idx has been selected for this package.

hash(node, h)[source]
ignored_attributes = re.compile('^.*_propagate$|^.*_satisfies$|^.*_set$|^compatible_libc$|^dependency_holds$|^external_conditions_hold$|^node_compiler$|^package_hash$|^root$|^track_dependencies$|^variant_default_value_from_cli$|^vir)

Regex for attributes that don’t need actions b/c they aren’t used to construct specs.

static make_node(*, pkg: str) NodeArgument[source]

Given a package name, returns the string representation of the “min_dupe_id” node in the ASP encoding.


pkg – name of a package

namespace(node, namespace)[source]
no_flags(node, flag_type)[source]
node_compiler_version(node, compiler, version)[source]
node_flag(node, flag_type, flag)[source]
node_flag_source(node, flag_type, source)[source]
node_os(node, os)[source]
node_platform(node, platform)[source]
node_target(node, target)[source]

Order compiler flags on specs in predefined order.

We order flags so that any node’s flags will take priority over those of its dependents. That is, the deepest node in the DAG’s flags will appear last on the compile line, in the order they were specified.

The solver determines which flags are on nodes; this routine imposes order afterwards.

static sort_fn(function_tuple)[source]

Ensure attributes are evaluated in the correct order.

hash attributes are handled first, since they imply entire concrete specs node attributes are handled next, since they instantiate nodes external_spec_selected attributes are handled last, so that external extensions can find the concrete specs on which they depend because all nodes are fully constructed before we consider which ones are external.

variant_value(node, name, value)[source]
version(node, version)[source]
virtual_on_edge(parent_node, provider_node, virtual)[source]
class spack.solver.asp.SpecFilter(factory: Callable[[], List[Spec]], is_usable: Callable[[Spec], bool], include: List[str], exclude: List[str])[source]

Bases: object

Given a method to produce a list of specs, this class can filter them according to different criteria.

static from_buildcache(configuration, include, exclude) SpecFilter[source]

Constructs a filter that takes the specs from the configured buildcaches.

static from_store(configuration, include, exclude) SpecFilter[source]

Constructs a filter that takes the specs from the current store.

is_selected(s: Spec) bool[source]
selected_specs() List[Spec][source]
exception spack.solver.asp.UnsatisfiableSpecError(msg)[source]

Bases: UnsatisfiableSpecError

There was an issue with the spec that was requested (i.e. a user error).

spack.solver.asp.WITH_RUNTIME = True

Enable the addition of a runtime node

spack.solver.asp.all_libcs() Set[Spec][source]

Return a set of all libc specs targeted by any configured compiler. If none, fall back to libc determined from the current Python process if dynamically linked.

spack.solver.asp.build_criteria_names(costs, arg_tuples)[source]

Construct an ordered mapping from criteria names to costs.

spack.solver.asp.build_priority_offset = 200

Priority offset for “build” criteria (regular criterio shifted to higher priority for specs we have to build)

spack.solver.asp.c_compiler_runs(compiler: Compiler) bool[source]

Ensure all packages mentioned in specs exist.


Return a control object with the default settings used in Spack

spack.solver.asp.extend_flag_list(flag_list, new_flags)[source]

Extend a list of flags, preserving order and precedence.

Add new_flags at the end of flag_list. If any flags in new_flags are already in flag_list, they are moved to the end so that they take higher precedence on the compile line.

spack.solver.asp.fixed_priority_offset = 100

Priority offset of “fixed” criteria (those w/o build criteria)

spack.solver.asp.high_fixed_priority_offset = 300

High fixed priority offset for criteria that supersede all build criteria

spack.solver.asp.libc_is_compatible(lhs: Spec, rhs: Spec) List[Spec][source]
spack.solver.asp.remove_node(spec: Spec, facts: List[AspFunction]) List[AspFunction][source]

Transformation that removes all “node” and “virtual_node” from the input list of facts.

spack.solver.asp.spec_with_name(spec, name)[source]

Context manager to temporarily set the name of a spec

spack.solver.asp.using_libc_compatibility() bool[source]

Returns True if we are currently using libc compatibility

spack.solver.core module

Low-level wrappers around clingo API.

class spack.solver.core.AspFunction(name: str, args: Tuple[Any, ...] | None = None)[source]

Bases: AspObject

A term in the ASP logic program


Return a clingo symbol for this function

class spack.solver.core.AspObject[source]

Bases: object

Object representing a piece of ASP code.

class spack.solver.core.NodeArgument(id: str, pkg: str)[source]

Bases: NamedTuple

Represents a node in the DAG

id: str

Alias for field number 0

pkg: str

Alias for field number 1

spack.solver.core.clingo() module[source]

Lazy imports the Python module for clingo, and returns it.

spack.solver.core.clingo_cffi() bool[source]

Returns True if clingo uses the CFFI interface

spack.solver.core.extract_args(model, predicate_name)[source]

Extract the arguments to predicates with the provided name from a model.

Pull out all the predicates with name predicate_name from the model, and return their intermediate representation.

spack.solver.core.fn = <spack.solver.core._AspFunctionBuilder object>

Global AspFunction builder


Returns an intermediate representation of clingo models for Spack’s spec builder.

Currently, transforms symbols from clingo models either to strings or to NodeArgument objects.


This will turn a clingo.Symbol into a string or NodeArgument, or a sequence of clingo.Symbol objects into a tuple of those objects.

spack.solver.core.parse_files(*args, **kwargs)[source]

Wrapper around clingo parse_files, that dispatches the function according to clingo API version.

spack.solver.core.parse_term(*args, **kwargs)[source]

Wrapper around clingo parse_term, that dispatches the function according to clingo API version.

spack.solver.counter module

class spack.solver.counter.Counter(specs: List[Spec], tests: bool)[source]

Bases: object

Computes the possible packages and the maximum number of duplicates allowed for each of them.

  • specs – abstract specs to concretize

  • tests – if True, add test dependencies to the list of possible packages

ensure_cache_values() None[source]

Ensure the cache values have been computed

possible_dependencies() Set[str][source]

Returns the list of possible dependencies

possible_packages_facts(gen: PyclingoDriver, fn) None[source]

Emit facts associated with the possible packages

possible_virtuals() Set[str][source]

Returns the list of possible virtuals

class spack.solver.counter.FullDuplicatesCounter(specs, tests)[source]

Bases: MinimalDuplicatesCounter

possible_packages_facts(gen, fn)[source]

Emit facts associated with the possible packages

class spack.solver.counter.MinimalDuplicatesCounter(specs, tests)[source]

Bases: NoDuplicatesCounter

possible_packages_facts(gen, fn)[source]

Emit facts associated with the possible packages

class spack.solver.counter.NoDuplicatesCounter(specs: List[Spec], tests: bool)[source]

Bases: Counter

possible_packages_facts(gen, fn)[source]

Emit facts associated with the possible packages