Skip to content

Commit 6681346

Browse files
authored
Add syntactic sugar for "strong preferences" and "conflicts" (#41832)
Currently requirements allow to express "strong preferences" and "conflicts" from configuration using a convoluted syntax: ```yaml packages: zlib-ng: require: # conflict on %clang - one_of: ["%clang", "@:"] # Strong preference for +shared - any_of: ["+shared", "@:"] ``` This PR adds syntactic sugar so that the same can be written as: ```yaml packages: zlib-ng: conflict: - "%clang" prefer: - "+shared" ``` Preferences written in this way are "stronger" that the ones documented at: - https://spack.readthedocs.io/en/latest/packages_yaml.html#package-preferences
1 parent ed9d495 commit 6681346

File tree

6 files changed

+358
-115
lines changed

6 files changed

+358
-115
lines changed

lib/spack/docs/packages_yaml.rst

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -487,6 +487,56 @@ present. For instance with a configuration like:
487487
488488
you will use ``mvapich2~cuda %gcc`` as an ``mpi`` provider.
489489

490+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
491+
Conflicts and strong preferences
492+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
493+
494+
If the semantic of requirements is too strong, you can also express "strong preferences" and "conflicts"
495+
from configuration files:
496+
497+
.. code-block:: yaml
498+
499+
packages:
500+
all:
501+
prefer:
502+
- '%clang'
503+
conflict:
504+
- '+shared'
505+
506+
The ``prefer`` and ``conflict`` sections can be used whenever a ``require`` section is allowed.
507+
The argument is always a list of constraints, and each constraint can be either a simple string,
508+
or a more complex object:
509+
510+
.. code-block:: yaml
511+
512+
packages:
513+
all:
514+
conflict:
515+
- spec: '%clang'
516+
when: 'target=x86_64_v3'
517+
message: 'reason why clang cannot be used'
518+
519+
The ``spec`` attribute is mandatory, while both ``when`` and ``message`` are optional.
520+
521+
.. note::
522+
523+
Requirements allow for expressing both "strong preferences" and "conflicts".
524+
The syntax for doing so, though, may not be immediately clear. For
525+
instance, if we want to prevent any package from using ``%clang``, we can set:
526+
527+
.. code-block:: yaml
528+
529+
packages:
530+
all:
531+
require:
532+
- one_of: ['%clang', '@:']
533+
534+
Since only one of the requirements must hold, and ``@:`` is always true, the rule above is
535+
equivalent to a conflict. For "strong preferences" we need to substitute the ``one_of`` policy
536+
with ``any_of``.
537+
538+
539+
490540
.. _package-preferences:
491541

492542
-------------------

lib/spack/spack/schema/packages.py

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,24 @@
5454
]
5555
}
5656

57+
prefer_and_conflict = {
58+
"type": "array",
59+
"items": {
60+
"oneOf": [
61+
{
62+
"type": "object",
63+
"additionalProperties": False,
64+
"properties": {
65+
"spec": {"type": "string"},
66+
"message": {"type": "string"},
67+
"when": {"type": "string"},
68+
},
69+
},
70+
{"type": "string"},
71+
]
72+
},
73+
}
74+
5775
permissions = {
5876
"type": "object",
5977
"additionalProperties": False,
@@ -85,6 +103,8 @@
85103
"additionalProperties": False,
86104
"properties": {
87105
"require": requirements,
106+
"prefer": prefer_and_conflict,
107+
"conflict": prefer_and_conflict,
88108
"version": {}, # Here only to warn users on ignored properties
89109
"target": {
90110
"type": "array",
@@ -133,6 +153,8 @@
133153
"additionalProperties": False,
134154
"properties": {
135155
"require": requirements,
156+
"prefer": prefer_and_conflict,
157+
"conflict": prefer_and_conflict,
136158
"version": {
137159
"type": "array",
138160
"default": [],
@@ -186,7 +208,6 @@
186208
}
187209
}
188210

189-
190211
#: Full schema with metadata
191212
schema = {
192213
"$schema": "http://json-schema.org/draft-07/schema#",

0 commit comments

Comments
 (0)