Type Utilities

The following helper functions can help you distinguish between the value type¹ of variables.

¹: In CMake all variable values are strings. See official documentation for details.

is_int

Determines if value is an integer.

is_int(22 a)
is_int("hi there" b)

message("${a}") # true
message("${b}") # false

is_float

Determines if value is a float point.

is_float(2.34 a)
is_float(2 b)
is_int("hi there" c)

message("${a}") # true
message("${b}") # false
message("${c}") # false

is_numeric

Determines if value is numeric.

is_numeric(22 a)
is_numeric(1.234 b)
is_numeric("hi there" c)

message("${a}") # true
message("${b}") # true
message("${c}") # false

is_bool

Determines if value is a boolean (true or false).

is_bool(true a)
is_bool(false b)
is_bool("on" c)

message("${a}") # true
message("${b}") # true
message("${c}") # false

is_bool_like

Determine if value is a boolean like. See CMake’s official documentation for additional details:

is_bool_like(true a)
is_bool_like("yes" b)
is_bool_like(-2 c)

message("${a}") # true
message("${b}") # true
message("${c}") # false

is_list

Determines if value is a list of values. See CMake’s official documentation for additional details.

is_list("aa;bbb;ccc" a)

set(my_list "aa;bbb;ccc")
is_list(my_list b)

set(my_other_list aa bbb ccc)
is_list(my_other_list c)

is_list("aa bbb ccc" d)

message("${a}") # true
message("${b}") # true
message("${c}") # true
message("${d}") # false

is_command

Determines if value is command, macro or function.

macro(my_macro)
endmacro()
is_command(my_macro a)

function(my_function)
endfunction()
is_command(my_function b)

set(my_var "")
is_command(my_var c)

message("${a}") # true
message("${b}") # true
message("${c}") # false

is_string

Determines if value is a string. In this context, a value is considered to be a string, if it is:

is_string("abc" a)

set(my_str "foo")
is_string(my_str b)

is_string(42 c)

message("${a}") # true
message("${b}") # true
message("${c}") # false

get_type

Determines the type of given value. Function assigns a string representation of the value’s type (int, float, bool, list, command, or string), to the output argument.

get_type("abc" a)
get_type("aaa;bbb;ccc" b)
get_type(true c)
get_type(12 d)

message("${a}") # string
message("${b}") # list
message("${c}") # bool
message("${c}") # int