How does one early-return from a function in CMake, as in the following example?
function do_the_thing(HAS_PROPERTY_A)
# don't do things that have property A when property A is disabled globally
if (PROPERTY_A_DISABLED AND HAS_PROPERTY_A)
# What do you put here to return?
endif()
# do things and implement magic
endfunction()
You use return()
(CMake manual page here), which returns from a function if called when in a function.
For example:
cmake_minimum_required(VERSION 3.0)
project(returntest)
# note: your function syntax was wrong - the function name goes
# after the parenthesis
function (do_the_thing HAS_PROPERTY_A)
if (HAS_PROPERTY_A)
message(STATUS "Early Return")
return()
endif()
message(STATUS "Later Return")
endfunction()
do_the_thing(TRUE)
do_the_thing(FALSE)
Results in:
$ cmake ../returntest
-- Early Return
-- Later Return
-- Configuring done
...
It works outside functions, too: If you call it from an include()
ed file, it returns to the includer, if you call if from an file via add_subdirectory()
, it'll return you to the parent file.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With