Snapcraft advanced grammar

Several fields in snapcraft.yaml are dependent on the architecture or the operating system being exposed to Snapcraft. This is accomplished using specific and advanced syntax within the YAML consumed by the snapcraft command.

This advanced grammar is made up of three nestable statements: on, to and try:

The on statement

    - on <selector>[,<selector>...]:
        <grammar>|<primitive>
    - else[ fail]:
        <grammar>|<primitive>

<primitive> may be either a list or a scalar, depending on the keyword.

The body of the on clause is taken into account if every (AND, not OR) selector is true for the build environment. Currently the only selectors supported are host architectures (e.g. amd64).

If the on clause doesn’t match and is immediately followed by an else clause, the else clause must be satisfied. An on clause without an else clause is considered satisfied even if no selector matched. The else fail form allows for the generation of an error if a to clause is not matched.

The to statement

    - [on <selector>[,<selector>...] ]to <selector>[,<selector>...]:
        <grammar>|<primitive>
    - else[ fail]:
        <grammar>|<primitive>

<primitive> may be either a list or a scalar, depending on the keyword.

The body of the to clause is taken into account if every (AND, not OR) selector is true for the target environment. Currently the only selectors supported are target architectures (e.g. armhf).

If the to clause doesn’t match and is immediately followed by an else clause, the else clause must be satisfied. A to clause without an else clause is considered satisfied even if no selector matched. The else fail form allows for the generation of an error if a to clause is not matched.

Optionally an ‘on’ statement can precede a 'to' in the same line to form a compound statement. Used this way, the selectors of both statements have to be true. That is to say, both the build environment and the target have to be true for the body of the clause to be taken into account.

The try statement

    - try:
        <grammar>|<primitive>
    - else:
        <grammar>|<primitive>

<primitive> may be either a list or a scalar, depending on the keyword.

Warning: The try statement does not work with builds using a base snap of core22 or later.

The body of the try clause is taken into account only when all primitives contained within it are valid (primitive validity is determined on a keyword-specific basis). If they are not all valid, and are immediately followed by else clauses, those are tried in order, and one of them must be satisfied. A try clause with no ‘else’ clause is considered satisfied even if it contains invalid primitives.

Example

parts:
  _part_name_:
    stage-packages:
    # libnuma1 package isn't available in armhf architecture, make it optional
    - try:
      - libnuma1
1 Like

Converted from the legacy documentation.

It would be really helpful if examples were provided for the usage of on and to.

2 Likes

This documentation is clear as mud. :face_with_hand_over_mouth: Please add some examples for on and to.

I am trying to figure out how to dump architecture-specific tarballs.

As I read it, “try” has been dropped from SC7, so should this page be updated? Or is there another page for SC7 grammar?

I’m still concerned that not all of this is valid under SC7 and core22.

I’ve added the following note, but I’d like @degville to take a look to figure out better words than I can do.

Thanks for adding this!

…and currently the advanced grammar doesn’t work for configuring plug-in lists-of-strings.