CMakePackage: convert variants to CMake arguments (#14376)

Add a 'define_from_variant` helper function to CMake-based Spack
packages to convert package variants into CMake arguments. For
example:

  args.append('-DFOO=%s' % ('ON' if '+foo' in self.spec else 'OFF'))

can be replaced with:

  args.append(self.define_from_variant('foo'))

The following conversions are handled automatically:

* Flag variants will be converted to CMake booleans
* Multivalued variants will be converted to semicolon-separated strings
* Other variant values are converted to CMake string arguments

This also adds a 'define' helper method to convert any variable to
a CMake argument. It has the same conversion rules as
'define_from_variant' (but operates directly on values rather than
requiring the user to supply the name of a package variant).
This commit is contained in:
Seth R. Johnson
2020-03-16 14:41:19 -04:00
committed by GitHub
parent 55d5adfecf
commit a8706cc89b
4 changed files with 161 additions and 16 deletions

View File

@@ -21,6 +21,14 @@ class CmakeClient(CMakePackage):
version('1.0', '4cb3ff35b2472aae70f542116d616e63')
variant(
'multi', description='',
values=any_combination_of('up', 'right', 'back').with_default('up')
)
variant('single', description='', default='blue',
values=('blue', 'red', 'green'), multi=False)
variant('truthy', description='', default=True)
callback_counter = 0
flipped = False