Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CMake: use of true/false or on/off or yes/no in generator expressions

Tags:

cmake

Is there any way to make it easy or possible to use true/false or on/off or yes/no in CMake's generator expressions, besides 1/0?

Context

As far as documentation is concerned, only 1 and 0 are recognized within generator expressions.

However, as far as the if command is concerned, 1, ON, YES, TRUE, Y are all considered boolean values.

This makes some things confusing; for example, one could use the option command to get an input from the user in the configuration phase, and then try to use its value within a generator expression; however, in this case, cmake would complain if the value is not 1 or 0. The same applies to boolean variables stored in CMake's cache.

SSCCE Example

cmake_minimum_required(VERSION 2.8.12)

add_custom_target(print
    ${CMAKE_COMMAND} -E echo $<1:hello> $<0:world>
    )

Create a CMakeLists.txt file with the above content, run cmake . and then make (generator expressions are only evaluated at build time). It will print hello, as expected.

However, the following example won't even work with the cmake command itself:

cmake_minimum_required(VERSION 2.8.12)

add_custom_target(print2
    ${CMAKE_COMMAND} -E echo $<true:hello> $<false:world>
    )

Here's its output (at configuration time):

-- Configuring done
CMake Error at CMakeLists.txt:3 (add_custom_target):
  Error evaluating generator expression:

    $<true:hello>

  Expression did not evaluate to a known generator expression


CMake Error at CMakeLists.txt:3 (add_custom_target):
  Error evaluating generator expression:

    $<false:world>

  Expression did not evaluate to a known generator expression


-- Generating done
-- Build files have been written to: /tmp/cmake

Again:

Is there any way to make it easy or possible to use true/false or on/off or yes/no in CMake's generator expressions, besides 1/0?

Or, alternatively, an easy way to convert a variable with one of these values to 1/0 so it could then be used in generator expressions?

like image 248
thiagowfx Avatar asked Dec 10 '16 19:12

thiagowfx


People also ask

What are generator expressions in CMake?

Introduction. Generator expressions are evaluated during build system generation to produce information specific to each build configuration.

What is Cache variable in CMake?

The CMake cache may be thought of as a configuration file. The first time CMake is run on a project, it produces a CMakeCache. txt file in the top directory of the build tree. CMake uses this file to store a set of global cache variables, whose values persist across multiple runs within a project build tree.

What is option in CMake?

Provide a boolean option that the user can optionally select. option(<variable> "<help_text>" [value]) If no initial <value> is provided, boolean OFF is the default value. If <variable> is already set as a normal or cache variable, then the command does nothing (see policy CMP0077 ).


1 Answers

This is what the $<BOOL:...> generator expression is for. See the start of the Logical Expressions section of the generator expression docs. Modifying your example:

cmake_minimum_required(VERSION 2.8.12)

add_custom_target(print2
    ${CMAKE_COMMAND} -E echo $<$<BOOL:true>:hello> $<$<BOOL:false>:world>
)
like image 196
Craig Scott Avatar answered Sep 21 '22 12:09

Craig Scott