How can I quickly return from a function in cmake?

How is one of the earliest returns 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() 
+6
source share
1 answer

You use return() (the CMake man page here ), which returns from the function if called when in the function.

For instance:

 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 also works outside of functions: if you call it from the include() ed file, it returns to inclusion, if you call it from the file via add_subdirectory() , it will return you to the parent file.

+5
source

Source: https://habr.com/ru/post/1013172/


All Articles