Skip to content

Toolchains: parse-time aliases for dependencies#50481

Merged
tgamblin merged 84 commits intodevelopfrom
features/toolchains
Jun 6, 2025
Merged

Toolchains: parse-time aliases for dependencies#50481
tgamblin merged 84 commits intodevelopfrom
features/toolchains

Conversation

@becker33
Copy link
Copy Markdown
Member

@becker33 becker33 commented May 15, 2025

This PR includes 2 primary components:

Conditional dependencies

Conditional dependencies can be expressed with a when= clause on the dependency edge attributes

For example:

hdf5 %[when=+fortran virtuals=fortran] gcc ^[when=^mpi, virtuals=mpi ] mpich

means that hdf5 depends on mpich for mpi when it depends on mpi, and depends on gcc for fortran when the fortran variant is turned on.

You may need to quote this on the CLI, like so:

> spack spec "hdf5 %[when=+fortran virtuals=fortran] gcc ^[when=^mpi, virtuals=mpi ] mpich"

Toolchains

Toolchains are parse-time aliases for dependencies. This allows complex dependency relationships to be encoded by simple aliases. The toolchains feature is designed to facilitate the use-case previously allowed by configuring compilers to have the fortran compiler from a different compiler set -- toolchains are however more general, and can apply to any dependencies.

Toolchains are configured in the toolchains config section, e.g.

toolchains:
  clang_gfortran: 
      - spec: %[virtuals=c] clang
        when: %c
      - spec: %[virtuals=cxx] clang
        when: %cxx
      - spec: %[virtuals=fortran] gcc
        when: %fortran
  gcc_mpich:
      - spec: %[virtuals=c] gcc
        when: %c
      - spec: %[virtuals=cxx] gcc
        when: %cxx
      - spec: %[virtuals=fortran] gcc
        when: %fortran
      - spec: %[virtuals=mpi] mpich
        when: %mpi

With this configuration, you can now use %clang_gfortran and %gcc_mpich as you would a compiler. You can add flags, e.g.:

gcc_mpich:
    - spec: cflags="-O3 -g"
    - spec: cxxflags="-O3 -g"
    - spec: fflags="-O3 -g"
    - spec: %[virtuals=c] gcc
      when: %c
    - spec: %[virtuals=cxx] gcc
      when: %cxx
    - spec: %[virtuals=fortran] gcc
      when: %fortran
    - spec: %[virtuals=mpi] mpich
      when: %mpi

The clang_gfortran toolchain is roughly equivalent to configuring a Spack v0.* compiler to have clang executables for c/cxx and gfortran for fortran. The gcc_mpich toolchain uses only gcc, but is extended to also include the mpi implementation to use.

You can currently only use % (direct dependency) in toolchain conditions, though we are working on enabling ^ conditions in a later PR.

You can write the same toolchains with conditional dependencies, but it's much harder to read:

toolchains:
  clang_gfortran: "%[when='%c' virtuals=c]clang %[when='%cxx' virtuals=cxx]clang %[when='%fortran' virtuals=fortran]gcc"
  gcc_mpich: "%[when='%c' virtuals=c]gcc %[when='%cxx' virtuals=cxx]gcc %[when='%fortran' virtuals=fortran]gcc %[when='%mpi' virtuals=mpi] mpich"

Some notes on using toolchains:

  • You likely want to use conditional dependencies as shown above. If you use unconditional dependencies, it will turn on variants to ensure the compiler can be used, and will fail for packages that don't depend on a given language.
  • For similar reasons, it's best to separately specify each condition for shared virtuals like c/cxx/fortran from the same package.

TODO:

  • unit tests for prefers/requires with conditional dependencies
  • unit tests for constraints with conditional dependencies (partially tested by concretization tests)
  • unit tests for satisfies with conditional dependencies (partially tested by concretization tests)
  • documentation

Loading
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

commands core PR affects Spack core functionality dependencies documentation Improvements or additions to documentation new-variant new-version shell-support stand-alone-tests Stand-alone (or smoke) tests for installed packages tests General test capability(ies)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants